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
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C
5 changed files with 97 additions and 23 deletions

@ -1,11 +1,16 @@
import { UseFetchOptions } from "nuxt/app";
const useApiFetch = async (url: string, options?: any) => {
const at = useCookie("rockfic_cookie", {default: undefined})
return useFetch("/api" + url, {
method: "get",
...options,
})
}
export default useApiFetch
import { UseFetchOptions } from "nuxt/app";
const useApiFetch = async (url: string, options?: any) => {
const at = useCookie("rockfic_cookie", { default: undefined });
const { token } = useAuth();
return useFetch("/api" + url, {
method: "get",
headers: {
...(options?.headers || {}),
Authorization: `Bearer ${token.value}`,
},
...options,
});
};
export default useApiFetch;

@ -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 });
}
}
});

@ -0,0 +1,3 @@
export default eventHandler((event) => {
})

@ -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 { User } from "~/models/user"
export default defineEventHandler(async (event) => {
let ahead = getCookie(event, "rockfic_cookie")
if(ahead) {
let toktok = (jwt.verify(ahead.replace("Bearer ", ""), useRuntimeConfig().jwt) as jwt.JwtPayload)
console.log(toktok)
let user = await User.findById(toktok.id as number).exec()
event.context.currentUser = user
}
})
import jwt from "jsonwebtoken";
import { User } from "~/models/user";
export default defineEventHandler(async (event) => {
let ahead = (getHeaders(event).authorization || "")?.replace("Bearer ", "");
console.log(`=------------------------${event.path}`);
console.log("h", getHeaders(event));
console.log(JSON.stringify(ahead));
if (ahead) {
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;
}
});