11import { NextRequest , NextResponse } from "next/server" ;
2+ import { cookies } from "next/headers" ;
23import { type Session , getGlobalConfig } from "./index" ;
34import jwt from "jsonwebtoken" ;
45
5- export const getSession = async ( req : NextRequest ) : Promise < Session | null > => {
6+ export const getSession = async ( ) : Promise < Session | null > => {
67 const config = getGlobalConfig ( ) ;
7- const token = req . cookies . get ( "AUTH_SESSION" ) ?. value ;
8- if ( ! token ) {
9- return null ;
10- }
8+ const cookieStore = await cookies ( ) ;
9+ const token = cookieStore . get ( "AUTH_SESSION" ) ?. value ;
10+ if ( ! token ) return null ;
1111
1212 try {
1313 return jwt . verify ( token , config . jwtSecret ) as Session ;
@@ -19,29 +19,32 @@ export const getSession = async (req: NextRequest): Promise<Session | null> => {
1919
2020export const signIn = async ( req : NextRequest ) : Promise < NextResponse > => {
2121 const config = getGlobalConfig ( ) ;
22- const session = await getSession ( req ) ;
22+ const session = await getSession ( ) ;
2323 if ( session ) {
24- return NextResponse . json (
25- { message : "Already signed in" } ,
26- { status : 200 } ,
27- ) ;
24+ return NextResponse . json ( { message : "Already signed in" } , { status : 200 } ) ;
2825 }
29-
3026 const signInURL = `https://discord.com/api/oauth2/authorize?client_id=${ config . clientId } &redirect_uri=${ encodeURIComponent ( config . redirectUri ) } &response_type=code&scope=${ config . scopes . join ( " " ) } ` ;
3127 return NextResponse . redirect ( signInURL , 302 ) ;
3228} ;
3329
34- export const signOut = async ( req : NextRequest ) : Promise < NextResponse > => {
30+ export const signOut = async ( ) : Promise < NextResponse > => {
3531 const config = getGlobalConfig ( ) ;
36- const session = await getSession ( req ) ;
37- if ( ! session ) {
32+ const cookieStore = await cookies ( ) ;
33+ const token = cookieStore . get ( "AUTH_SESSION" ) ?. value ;
34+ if ( ! token ) {
3835 return NextResponse . json ( { message : "Not signed in" } , { status : 401 } ) ;
3936 }
4037
38+ try {
39+ jwt . verify ( token , config . jwtSecret ) ;
40+ } catch {
41+ return NextResponse . json ( { message : "Invalid session" } , { status : 401 } ) ;
42+ }
43+
4144 const response = NextResponse . json (
4245 { message : "Signed out successfully" } ,
43- { status : 200 } ,
46+ { status : 200 }
4447 ) ;
4548 response . cookies . delete ( "AUTH_SESSION" ) ;
4649 return response ;
47- } ;
50+ } ;
0 commit comments