Skip to content

Commit

Permalink
Move API route
Browse files Browse the repository at this point in the history
  • Loading branch information
joao-vasconcelos committed Dec 27, 2023
1 parent 2fadff8 commit 2228d10
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
File renamed without changes.
36 changes: 21 additions & 15 deletions nextjs/authentication/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,33 @@ import jwt from 'jsonwebtoken';
/* * */

export async function verifyAuthentication(request) {
//
try {
//

//
// 1. Verify next-auth session
//
// 1. Verify next-auth session

const session = await auth();
if (session?.user?.id) return session.user.id;
const session = await auth();
if (session?.user?.id) return session.user.id;

//
// 2. Verify JWT token
//
// 2. Verify JWT token

const authorizationHeader = request.headers?.get('Authorization');
if (!authorizationHeader?.length) return false;
const authorizationHeader = request.headers?.get('Authorization');
if (!authorizationHeader?.length) return false;

const rawJwtToken = authorizationHeader?.split(' ')[1];
if (!rawJwtToken) return false;
const rawJwtToken = authorizationHeader?.split(' ')[1];
if (!rawJwtToken) return false;

const decodedJwtToken = jwt.verify(rawJwtToken, process.env.JWT_SIGN_SECRET);
if (decodedJwtToken?.user_id) return decodedJwtToken.user_id;
const decodedJwtToken = jwt.verify(rawJwtToken, process.env.JWT_SIGN_SECRET);
if (decodedJwtToken?.user_id) return decodedJwtToken.user_id;
//

return false;
return false;

//
//
} catch (error) {
console.log(error);
return false;
}
}

0 comments on commit 2228d10

Please sign in to comment.