Skip to content

Commit

Permalink
feat(db): make db more dynamic
Browse files Browse the repository at this point in the history
Signed-off-by: Vinayak Kulkarni <19776877+vinayakkulkarni@users.noreply.github.com>
  • Loading branch information
vinayakkulkarni committed Feb 19, 2024
1 parent 9e2d6b5 commit af42b88
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
9 changes: 4 additions & 5 deletions server/routes/login/github/callback.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default defineEventHandler(async (event) => {

// Execute the query
const [existingUser]: [DatabaseUser?] =
await sql`SELECT * FROM "user" WHERE github_id = ${githubUser.id}`;
await sql`SELECT * FROM "users" WHERE oauth_id = ${githubUser.id} AND oauth_provider = 'github' LIMIT 1`;
// Check if a user exists
if (existingUser) {
const session = await lucia.createSession(existingUser.id, {});
Expand All @@ -35,18 +35,17 @@ export default defineEventHandler(async (event) => {
);
return sendRedirect(event, '/');
}
console.log('user not found, creating user');
// Generate a new user id
const userId = generateId(15);
// Execute the query
const payload = {
id: userId,
github_id: githubUser.id,
oauth_provider: 'github',
oauth_id: githubUser.id,
username: githubUser.login,
avatar_url: githubUser.avatar_url,
};
await sql`insert into user ${sql(payload, 'id', 'github_id', 'username', 'avatar_url')}`;
console.log('user created');
await sql`insert into users ${sql(payload, 'id', 'oauth_provider', 'oauth_id', 'username', 'avatar_url')}`;

const session = await lucia.createSession(userId, {});
appendHeader(
Expand Down
2 changes: 1 addition & 1 deletion server/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const lucia = new Lucia(adapter, {
getUserAttributes: (attributes) => {
return {
username: attributes.username,
githubId: attributes.github_id,
githubId: attributes.oauth_id,
avatarUrl: attributes.avatar_url,
};
},
Expand Down
3 changes: 2 additions & 1 deletion server/utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const sql = postgres(config.database.url, {
export interface DatabaseUser {
id: string;
username: string;
github_id: number;
oauth_provider: string;
oauth_id: number;
avatar_url: string;
}

0 comments on commit af42b88

Please sign in to comment.