Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
albinmedoc committed Dec 2, 2024
1 parent fd44cae commit c3a9b59
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,4 @@ You configure this using environment variables.

`HEADER_NAME`: The name of the headers that contains the full name of the user

`HEADER_EMAIL`: The name of the headers that contains the email of the user
`HEADER_EMAIL`: The name of the headers that contains the email of the user
4 changes: 2 additions & 2 deletions src/lib/server/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getConfig } from "$lib/server/config";
import { LegacyScrypt } from "lucia";
import type { User } from "@prisma/client";

type UserMinimal = Pick<User, 'username' | 'email' | 'name'>;
type UserMinimal = Pick<User, "username" | "email" | "name">;

export const createUser = async (user: UserMinimal, role: Role, password: string, signupTokenId?: string) => {
const config = await getConfig();
Expand Down Expand Up @@ -68,4 +68,4 @@ export const createUser = async (user: UserMinimal, role: Role, password: string
}

return newUser;
}
};
2 changes: 1 addition & 1 deletion src/routes/account/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const load: PageServerLoad = async ({ locals, request }) => {
redirect(302, `/login?ref=/account`);
}

const isProxyUser = env.HEADER_AUTH_ENABLED == 'true' && !!request.headers.get(env.HEADER_USERNAME);
const isProxyUser = env.HEADER_AUTH_ENABLED == "true" && !!request.headers.get(env.HEADER_USERNAME);

return {
user,
Expand Down
30 changes: 17 additions & 13 deletions src/routes/login/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,38 @@ export const load: PageServerLoad = async ({ locals, request, cookies }) => {
}

/* Header authentication */
if (env.HEADER_AUTH_ENABLED == 'true') {
if (env.HEADER_AUTH_ENABLED == "true") {
const username = request.headers.get(env.HEADER_USERNAME);
if (username) {
let user = await client.user.findUnique({
select: {
id: true,
id: true
},
where: {
username: username
}
});

if (!user) {
if(!env.HEADER_EMAIL || !env.HEADER_NAME) {
if (!env.HEADER_EMAIL || !env.HEADER_NAME) {
return fail(400, { username: username, password: "", incorrect: true });
}
const name = request.headers.get(env.HEADER_NAME);
const email = request.headers.get(env.HEADER_EMAIL);
if(!name || !email) {
if (!name || !email) {
return fail(400, { username: username, password: "", incorrect: true });
}
const userCount = await client.user.count();
user = await createUser({
username: username,
email: email,
name: name
}, userCount > 0 ? Role.USER : Role.ADMIN, "");

user = await createUser(
{
username: username,
email: email,
name: name
},
userCount > 0 ? Role.USER : Role.ADMIN,
""
);

if (config.defaultGroup) {
await client.userGroupMembership.create({
data: {
Expand All @@ -60,7 +64,7 @@ export const load: PageServerLoad = async ({ locals, request, cookies }) => {
});
}
}

const session = await auth.createSession(user.id, {});
const sessionCookie = auth.createSessionCookie(session.id);
cookies.set(sessionCookie.name, sessionCookie.value, {
Expand Down Expand Up @@ -121,4 +125,4 @@ export const actions: Actions = {
return fail(400, { username: loginData.data.username, password: "", incorrect: true });
}
}
};
};
15 changes: 10 additions & 5 deletions src/routes/signup/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,16 @@ export const actions: Actions = {

const userCount = await client.user.count();
try {
const user = await createUser({
username: signupData.data.username,
email: signupData.data.email,
name: signupData.data.name
}, userCount > 0 ? Role.USER : Role.ADMIN, signupData.data.password, signupData.data.tokenId);
const user = await createUser(
{
username: signupData.data.username,
email: signupData.data.email,
name: signupData.data.name
},
userCount > 0 ? Role.USER : Role.ADMIN,
signupData.data.password,
signupData.data.tokenId
);

const session = await auth.createSession(user.id, {});
const sessionCookie = auth.createSessionCookie(session.id);
Expand Down

0 comments on commit c3a9b59

Please sign in to comment.