refactor(api): refactor app to use pure jwts instead of cookies
cookies are no longer used
This commit is contained in:
parent
b5f5edbcc5
commit
2b233364a1
@ -1,11 +1,16 @@
|
|||||||
import { UseFetchOptions } from "nuxt/app";
|
import { UseFetchOptions } from "nuxt/app";
|
||||||
|
|
||||||
const useApiFetch = async (url: string, options?: any) => {
|
const useApiFetch = async (url: string, options?: any) => {
|
||||||
const at = useCookie("rockfic_cookie", {default: undefined})
|
const at = useCookie("rockfic_cookie", { default: undefined });
|
||||||
return useFetch("/api" + url, {
|
const { token } = useAuth();
|
||||||
method: "get",
|
return useFetch("/api" + url, {
|
||||||
...options,
|
method: "get",
|
||||||
})
|
headers: {
|
||||||
}
|
...(options?.headers || {}),
|
||||||
|
Authorization: `Bearer ${token.value}`,
|
||||||
export default useApiFetch
|
},
|
||||||
|
...options,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useApiFetch;
|
||||||
|
49
server/api/auth/login.post.ts
Normal file
49
server/api/auth/login.post.ts
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import mongoose from "mongoose";
|
||||||
|
import jwt from "jsonwebtoken";
|
||||||
|
import { IUser, User } from "~/models/user";
|
||||||
|
|
||||||
|
export default eventHandler(async (event) => {
|
||||||
|
const wrongMsg = "wrong credentials";
|
||||||
|
let reqbody = await readBody(event);
|
||||||
|
let user = await User.findOne({ username: reqbody.username }).exec();
|
||||||
|
console.log("USER -> ", user);
|
||||||
|
console.log("conn ->", mongoose.connection);
|
||||||
|
let cok = getHeader(event, "Authorization")?.replace("Bearer ", "");
|
||||||
|
if (!cok) {
|
||||||
|
if (!user) {
|
||||||
|
throw createError({ statusCode: 401, message: wrongMsg });
|
||||||
|
}
|
||||||
|
if (user.banned) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 401,
|
||||||
|
message: "This account has been banned.",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (user.validPassword(reqbody.password)) {
|
||||||
|
if (!user.auth.emailVerified) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 401,
|
||||||
|
message:
|
||||||
|
'Account inactive!<br><a href="/activate/resend">Resend verification</a>?',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
let tok = user.generateToken(useRuntimeConfig().jwt);
|
||||||
|
// setCookie(event, "rockfic_cookie", tok);
|
||||||
|
return {
|
||||||
|
user,
|
||||||
|
token: tok,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
throw createError({ statusCode: 401, message: wrongMsg });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (jwt.verify(cok, useRuntimeConfig().jwt)) {
|
||||||
|
throw createError({
|
||||||
|
statusCode: 405,
|
||||||
|
message: "Already logged in.",
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
throw createError({ statusCode: 401, message: wrongMsg });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
3
server/api/auth/register.post.ts
Normal file
3
server/api/auth/register.post.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export default eventHandler((event) => {
|
||||||
|
|
||||||
|
})
|
10
server/api/auth/session.get.ts
Normal file
10
server/api/auth/session.get.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
export default eventHandler((event) => {
|
||||||
|
if (event.context.currentUser) {
|
||||||
|
return {
|
||||||
|
token: getHeader(event, "Authorization"),
|
||||||
|
user: event.context.currentUser,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
throw createError({ statusCode: 400, message: "unauthenticated" });
|
||||||
|
}
|
||||||
|
});
|
@ -1,12 +1,19 @@
|
|||||||
import jwt from "jsonwebtoken"
|
import jwt from "jsonwebtoken";
|
||||||
import { User } from "~/models/user"
|
import { User } from "~/models/user";
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
let ahead = getCookie(event, "rockfic_cookie")
|
let ahead = (getHeaders(event).authorization || "")?.replace("Bearer ", "");
|
||||||
if(ahead) {
|
console.log(`=------------------------${event.path}`);
|
||||||
let toktok = (jwt.verify(ahead.replace("Bearer ", ""), useRuntimeConfig().jwt) as jwt.JwtPayload)
|
console.log("h", getHeaders(event));
|
||||||
console.log(toktok)
|
console.log(JSON.stringify(ahead));
|
||||||
let user = await User.findById(toktok.id as number).exec()
|
if (ahead) {
|
||||||
event.context.currentUser = user
|
console.log("ahead", ahead);
|
||||||
}
|
let toktok = jwt.verify(
|
||||||
})
|
ahead,
|
||||||
|
// ahead.replace("Bearer ", ""),
|
||||||
|
useRuntimeConfig().jwt,
|
||||||
|
) as jwt.JwtPayload;
|
||||||
|
let user = await User.findById(toktok.id as number).exec();
|
||||||
|
event.context.currentUser = user;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user