Skip to content

Commit

Permalink
Finish user management at phoenix (#18)
Browse files Browse the repository at this point in the history
*  #15: adds user management operations register and login

* #15: changes schema for token in header

* refactors some code
  • Loading branch information
utkarsh867 authored Oct 12, 2019
1 parent b04eefb commit 04b0333
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
PRISMA_HOST=
PRISMA_SECRET=
PRISMA_SECRET=
GOOGLE_CLIENT_ID=
2 changes: 1 addition & 1 deletion .prettierrc.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
trailingComma: "es5"
tabWidth: 4
tabWidth: 2
semi: true
singleQuote: false
155 changes: 153 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"apollo-server-express": "^2.9.6",
"dotenv": "^8.1.0",
"express": "^4.17.1",
"google-auth-library": "^5.4.1",
"graphql": "^14.5.8",
"jsonwebtoken": "^8.5.1",
"pg": "^7.12.1",
Expand Down
86 changes: 72 additions & 14 deletions src/mutations/user.ts
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 };
Loading

0 comments on commit 04b0333

Please sign in to comment.