Skip to content

Commit

Permalink
feat: nice auth route implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Aug 1, 2023
1 parent 6061506 commit ad4e035
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/lib/types/auth.ts
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;
}
42 changes: 42 additions & 0 deletions src/routes/auth/login.ts
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,
});
};
30 changes: 30 additions & 0 deletions src/routes/auth/logout.ts
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",
})
)
);
});
}
57 changes: 57 additions & 0 deletions src/routes/auth/signup.ts
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,
});
};

0 comments on commit ad4e035

Please sign in to comment.