-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finish user management at phoenix (#18)
* #15: adds user management operations register and login * #15: changes schema for token in header * refactors some code
- Loading branch information
1 parent
b04eefb
commit 04b0333
Showing
9 changed files
with
280 additions
and
26 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
PRISMA_HOST= | ||
PRISMA_SECRET= | ||
PRISMA_SECRET= | ||
GOOGLE_CLIENT_ID= |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
trailingComma: "es5" | ||
tabWidth: 4 | ||
tabWidth: 2 | ||
semi: true | ||
singleQuote: false |
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 |
---|---|---|
@@ -1,19 +1,77 @@ | ||
import * as env from "dotenv"; | ||
import { OAuth2Client, VerifyIdTokenOptions } from "google-auth-library"; | ||
import { User } from "../generated/prisma-client"; | ||
import { TokenPayload } from "google-auth-library/build/src/auth/loginticket"; | ||
|
||
env.config(); | ||
|
||
const client = new OAuth2Client(process.env.GOOGLE_CLIENT_ID); | ||
|
||
const createUser = ( | ||
parent, | ||
{ username, email, image_url, phone, first_name, last_name, room_no }, | ||
ctx | ||
parent, | ||
{ username, email, image_url, phone, first_name, last_name, room_no }, | ||
ctx | ||
) => | ||
ctx.prisma.createUser({ | ||
username, | ||
email, | ||
image_url, | ||
phone, | ||
first_name, | ||
last_name, | ||
room_no, | ||
}); | ||
ctx.prisma.createUser({ | ||
username, | ||
email, | ||
image_url, | ||
phone, | ||
first_name, | ||
last_name, | ||
room_no, | ||
}); | ||
|
||
const deleteUser = (parent, { username }, ctx) => | ||
ctx.prisma.deleteUser({ username }); | ||
ctx.prisma.deleteUser({ username }); | ||
|
||
const login = async (parent, {}, ctx) => { | ||
try { | ||
await client.verifyIdToken({ | ||
idToken: ctx.token, | ||
audience: process.env.GOOGLE_CLIENT_ID, | ||
}); | ||
} catch (e) { | ||
return { token: null, login_status: false, register: false }; | ||
} | ||
const ticket = await client.verifyIdToken({ | ||
idToken: ctx.token, | ||
audience: process.env.GOOGLE_CLIENT_ID, | ||
}); | ||
const payload: TokenPayload = ticket.getPayload(); | ||
if (payload && payload.hd !== "connect.hku.hk") | ||
return { token: null, login_status: false, register: false }; | ||
const user: User = await ctx.prisma.user({ email: payload.email }); | ||
if (user === null) | ||
return { token: null, login_status: false, register: true }; | ||
return { token: ctx.token, login_status: true, register: false }; | ||
}; | ||
|
||
const register = async (parent, { user }, ctx) => { | ||
try { | ||
await client.verifyIdToken({ | ||
idToken: ctx.token, | ||
audience: process.env.GOOGLE_CLIENT_ID, | ||
}); | ||
} catch (e) { | ||
console.log(e); | ||
return null; | ||
} | ||
const ticket = await client.verifyIdToken({ | ||
idToken: ctx.token, | ||
audience: process.env.GOOGLE_CLIENT_ID, | ||
}); | ||
const payload: TokenPayload = ticket.getPayload(); | ||
if (payload && payload.hd !== "connect.hku.hk") return null; | ||
return ctx.prisma.createUser({ | ||
username: user.username, | ||
email: payload.email, | ||
image_url: payload.picture, | ||
phone: user.phone, | ||
first_name: payload.given_name, | ||
last_name: payload.family_name, | ||
room_no: user.room_no, | ||
}); | ||
}; | ||
|
||
export { createUser, deleteUser }; | ||
export { createUser, deleteUser, login, register }; |
Oops, something went wrong.