-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: nice auth route implementation
- Loading branch information
Showing
4 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export interface LoginBody { | ||
username: string; | ||
password: string; | ||
} | ||
|
||
export interface RegisterBody { | ||
username: string; | ||
email: string; | ||
password: string; | ||
passwordConfirm: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { auth } from "@/lib/auth/lucia"; | ||
import type { LoginBody } from "@/lib/types/auth"; | ||
import { responseHeaders } from "@/lib/responseHeaders"; | ||
|
||
export const login = async (request: Request): Promise<Response> => { | ||
const body = (await request.json()) as LoginBody; | ||
const { username, password } = body; | ||
|
||
if (!username || !password) { | ||
return new Response( | ||
JSON.stringify({ | ||
success: false, | ||
status: "error", | ||
error: "400 Bad Request", | ||
}), | ||
{ | ||
headers: responseHeaders, | ||
} | ||
); | ||
} | ||
|
||
const user = await auth.useKey( | ||
"username", | ||
username.toLowerCase(), | ||
password | ||
); | ||
|
||
const newSession = await auth.createSession({ | ||
userId: user.userId, | ||
attributes: {}, | ||
}); | ||
|
||
const sessionCookie = auth.createSessionCookie(newSession); | ||
|
||
return new Response(null, { | ||
headers: { | ||
Location: "/", | ||
"Set-Cookie": sessionCookie.serialize(), | ||
}, | ||
status: 302, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { auth } from "@/lib/auth/lucia"; | ||
|
||
export async function logout(request: Request): Promise<Response> { | ||
const authRequest = auth.handleRequest(request); | ||
const session = await authRequest.validate(); | ||
|
||
if (!session) { | ||
return new Response( | ||
JSON.stringify({ | ||
success: false, | ||
status: "error", | ||
error: "401 Unauthorized", | ||
}) | ||
); | ||
} | ||
|
||
await auth.invalidateSession(session.sessionId); | ||
authRequest.setSession(null); | ||
|
||
return new Promise((resolve) => { | ||
resolve( | ||
new Response( | ||
JSON.stringify({ | ||
success: true, | ||
status: "ok", | ||
}) | ||
) | ||
); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { auth } from "@/lib/auth/lucia"; | ||
import type { RegisterBody } from "@/lib/types/auth"; | ||
import { responseHeaders } from "@/lib/responseHeaders"; | ||
|
||
export const login = async (request: Request): Promise<Response> => { | ||
const body = (await request.json()) as RegisterBody; | ||
const { username, password, email, passwordConfirm } = body; | ||
|
||
if (!username || !password || !email || !passwordConfirm) { | ||
return new Response( | ||
JSON.stringify({ | ||
success: false, | ||
status: "error", | ||
error: "400 Bad Request", | ||
}), | ||
{ | ||
headers: responseHeaders, | ||
} | ||
); | ||
} | ||
|
||
const user = await auth.createUser({ | ||
key: { | ||
providerId: "username", | ||
providerUserId: username.toLowerCase(), | ||
password, | ||
}, | ||
attributes: { | ||
username, | ||
email, | ||
email_verified: 0, | ||
date_joined: new Date(), | ||
verified: 0, | ||
role: "USER", | ||
username_colour: null, | ||
avatar_url: null, | ||
banner_url: null, | ||
pronouns: null, | ||
bio: null, | ||
}, | ||
}); | ||
|
||
const newSession = await auth.createSession({ | ||
userId: user.userId, | ||
attributes: {}, | ||
}); | ||
|
||
const sessionCookie = auth.createSessionCookie(newSession); | ||
|
||
return new Response(null, { | ||
headers: { | ||
Location: "/", | ||
"Set-Cookie": sessionCookie.serialize(), | ||
}, | ||
status: 302, | ||
}); | ||
}; |