-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creating backend functions for user management (#322)
Co-authored-by: Katie Campbell Downie <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Katie Campbell Downie <[email protected]>
- Loading branch information
1 parent
68129de
commit e069eea
Showing
7 changed files
with
120 additions
and
25 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,14 @@ | ||
import NextAuth from "next-auth"; | ||
declare module "next-auth" { | ||
interface User { | ||
id: string; | ||
username?: string | null; | ||
firstName?: string | null; | ||
lastName?: string | null; | ||
} | ||
|
||
interface Session { | ||
user: User; | ||
} | ||
} | ||
export default NextAuth; |
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,67 @@ | ||
"use server"; | ||
|
||
import { getDbClient } from "./dbClient"; | ||
const dbClient = getDbClient(); | ||
|
||
/** | ||
* Adds a user to the user_management table if they do not already exist. | ||
* Uses data extracted from the JWT token. | ||
* @param userToken - The user data from the JWT token. | ||
* @param userToken.id - The user ID from the JWT token. | ||
* @param userToken.username - The username from the JWT token. | ||
* @param userToken.email - The email from the JWT token. | ||
* @param userToken.firstName - The first name from the JWT token. | ||
* @param userToken.lastName - The last name from the JWT token. | ||
* @returns The newly added user or an empty result if already exists. | ||
*/ | ||
export async function addUserIfNotExists(userToken: { | ||
id: string; | ||
username: string; | ||
email: string; | ||
firstName: string; | ||
lastName: string; | ||
}) { | ||
if (!userToken || !userToken.username) { | ||
console.error("Invalid user token. Cannot add user."); | ||
return; | ||
} | ||
|
||
const { id, username, email, firstName, lastName } = userToken; | ||
const userIdentifier = username || email; | ||
|
||
try { | ||
console.log("Checking if user exists:", id); | ||
|
||
const checkUserQuery = `SELECT username FROM user_management WHERE username = $1;`; | ||
const userExists = await dbClient.query(checkUserQuery, [userIdentifier]); | ||
|
||
if (userExists.rows.length > 0) { | ||
console.log("User already exists in user_management:", id); | ||
return userExists.rows[0]; | ||
} | ||
|
||
console.log("User not found. Proceeding to insert:", id); | ||
|
||
// TODO: Update the role based on the user's group in Keycloak | ||
const qc_role = "super-admin"; | ||
|
||
const insertUserQuery = ` | ||
INSERT INTO user_management (username, qc_role, first_name, last_name) | ||
VALUES ($1, $2, $3, $4) | ||
RETURNING username, qc_role, first_name, last_name; | ||
`; | ||
|
||
const result = await dbClient.query(insertUserQuery, [ | ||
userIdentifier, | ||
qc_role, | ||
firstName, | ||
lastName, | ||
]); | ||
|
||
console.log("User added to user_management", id); | ||
return result.rows[0]; | ||
} catch (error) { | ||
console.error("Error adding user to user_management:", error); | ||
throw error; | ||
} | ||
} |
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
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