cleanup
This commit is contained in:
@@ -1,6 +1,3 @@
|
|||||||
MONGO_CONNECTION_STRING=mongodb+srv://default:[email protected]/default?retryWrites=true&w=majority
|
MONGO_CONNECTION_STRING=mongodb+srv://default:[email protected]/default?retryWrites=true&w=majority
|
||||||
|
|
||||||
JWT_TOKEN_SECRET=afajdslfwk1@lkkasdfl21ASDF!2
|
JWT_TOKEN_SECRET=afajdslfwk1@lkkasdfl21ASDF!2
|
||||||
|
|
||||||
GOOGLE_AUTH_CLIENT_ID=423533244953-banligobgbof8hg89i6cr1l7u0p7c2pk.apps.googleusercontent.com
|
|
||||||
GOOGLE_AUTH_CLIENT_SECRET=
|
|
||||||
@@ -25,5 +25,8 @@
|
|||||||
"nodemon": "^2.0.15",
|
"nodemon": "^2.0.15",
|
||||||
"ts-node": "^10.7.0",
|
"ts-node": "^10.7.0",
|
||||||
"typescript": "^4.6.2"
|
"typescript": "^4.6.2"
|
||||||
}
|
},
|
||||||
|
"workspaces": [
|
||||||
|
"packages/*"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
+32
-33
@@ -1,18 +1,18 @@
|
|||||||
import { GoogleUserInfo, User } from "@nirvana/core/models";
|
import { GoogleUserInfo, User } from '@nirvana/core/models';
|
||||||
import { JwtClaims, authCheck } from "../middleware/auth";
|
import { JwtClaims, authCheck } from '../middleware/auth';
|
||||||
import express, { Application, Request, Response } from "express";
|
import express, { Application, Request, Response } from 'express';
|
||||||
|
|
||||||
import LoginResponse from "../../core/responses/login.response";
|
import LoginResponse from '../../core/responses/login.response';
|
||||||
import { OAuth2Client } from "google-auth-library";
|
import { OAuth2Client } from 'google-auth-library';
|
||||||
import { ObjectID } from "bson";
|
import { ObjectID } from 'bson';
|
||||||
import { ObjectId } from "mongodb";
|
import { ObjectId } from 'mongodb';
|
||||||
import UserDetailsResponse from "../../core/responses/userDetails.response";
|
import UserDetailsResponse from '../../core/responses/userDetails.response';
|
||||||
import { UserService } from "../services/user.service";
|
import { UserService } from '../services/user.service';
|
||||||
import { UserStatus } from "../../core/models/user.model";
|
import { UserStatus } from '../../core/models/user.model';
|
||||||
import { collections } from "../services/database.service";
|
import { collections } from '../services/database.service';
|
||||||
import { loadConfig } from "../config";
|
import { loadConfig } from '../config';
|
||||||
|
|
||||||
const jwt = require("jsonwebtoken");
|
const jwt = require('jsonwebtoken');
|
||||||
|
|
||||||
const config = loadConfig();
|
const config = loadConfig();
|
||||||
|
|
||||||
@@ -24,20 +24,20 @@ export default function getUserRoutes() {
|
|||||||
router.use(express.json());
|
router.use(express.json());
|
||||||
|
|
||||||
// get user details based on id token
|
// get user details based on id token
|
||||||
router.get("/", authCheck, getUserDetails);
|
router.get('/', authCheck, getUserDetails);
|
||||||
|
|
||||||
router.get("/login", login);
|
router.get('/login', login);
|
||||||
|
|
||||||
router.get("/authcheck", authCheck, handleAuthCheck);
|
router.get('/authcheck', authCheck, handleAuthCheck);
|
||||||
|
|
||||||
return router;
|
return router;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleAuthCheck(req: Request, res: Response) {
|
async function handleAuthCheck(req: Request, res: Response) {
|
||||||
try {
|
try {
|
||||||
res.status(200).json("You are good to go!");
|
res.status(200).json('You are good to go!');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(401).json("Unauthorized");
|
res.status(401).json('Unauthorized');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ async function getUserDetails(req: Request, res: Response) {
|
|||||||
|
|
||||||
user
|
user
|
||||||
? res.status(200).json(new UserDetailsResponse(user))
|
? res.status(200).json(new UserDetailsResponse(user))
|
||||||
: res.status(404).json("No such user");
|
: res.status(404).json('No such user');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
res.status(500).json(`Problem with signing user up or logging in`);
|
res.status(500).json(`Problem with signing user up or logging in`);
|
||||||
@@ -65,14 +65,14 @@ async function login(req: Request, res: Response) {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const ticket = await client.verifyIdToken({
|
const ticket = await client.verifyIdToken({
|
||||||
idToken: (id_token as string) ?? "",
|
idToken: (id_token as string) ?? '',
|
||||||
audience: config.GOOGLE_AUTH_CLIENT_ID, // Specify the CLIENT_ID of the app that accesses the backend
|
audience: config.GOOGLE_AUTH_CLIENT_ID, // Specify the CLIENT_ID of the app that accesses the backend
|
||||||
});
|
});
|
||||||
const googleUserId = ticket.getPayload()?.sub as string;
|
const googleUserId = ticket.getPayload()?.sub as string;
|
||||||
const email = ticket.getPayload()?.email as string;
|
const email = ticket.getPayload()?.email as string;
|
||||||
|
|
||||||
if (!googleUserId || !email) {
|
if (!googleUserId || !email) {
|
||||||
res.status(401).json("no google account found");
|
res.status(401).json('no google account found');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,15 +82,14 @@ async function login(req: Request, res: Response) {
|
|||||||
// if no user found, then go ahead and create user
|
// if no user found, then go ahead and create user
|
||||||
if (!user) {
|
if (!user) {
|
||||||
if (!access_token) {
|
if (!access_token) {
|
||||||
res.status(400).json("No access token provided");
|
res.status(400).json('No access token provided');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get google user info from access token
|
// get google user info from access token
|
||||||
const userInfo: GoogleUserInfo =
|
const userInfo: GoogleUserInfo = await UserService.getGoogleUserInfoWithAccessToken(
|
||||||
await UserService.getGoogleUserInfoWithAccessToken(
|
access_token as string,
|
||||||
access_token as string
|
);
|
||||||
);
|
|
||||||
|
|
||||||
// create initial user model object
|
// create initial user model object
|
||||||
|
|
||||||
@@ -103,7 +102,7 @@ async function login(req: Request, res: Response) {
|
|||||||
new Date(),
|
new Date(),
|
||||||
userInfo.picture,
|
userInfo.picture,
|
||||||
userInfo.verifiedEmail,
|
userInfo.verifiedEmail,
|
||||||
userInfo.locale
|
userInfo.locale,
|
||||||
);
|
);
|
||||||
|
|
||||||
// create user if not exists
|
// create user if not exists
|
||||||
@@ -120,18 +119,18 @@ async function login(req: Request, res: Response) {
|
|||||||
email: newUser.email,
|
email: newUser.email,
|
||||||
name: newUser.name,
|
name: newUser.name,
|
||||||
},
|
},
|
||||||
config.JWT_TOKEN_SECRET
|
config.JWT_TOKEN_SECRET,
|
||||||
);
|
);
|
||||||
|
|
||||||
insertResult
|
insertResult
|
||||||
? res.status(200).json(new LoginResponse(jwtToken, newUser))
|
? res.status(200).json(new LoginResponse(jwtToken, newUser))
|
||||||
: res.status(500).json("Failed to create account, already exists");
|
: res.status(500).json('Failed to create account, already exists');
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// create jwt token with new user info
|
// create jwt token with existing user info
|
||||||
const jwtToken = jwt.sign(
|
const existingUserJwtToken = jwt.sign(
|
||||||
{
|
{
|
||||||
userId: user._id,
|
userId: user._id,
|
||||||
googleUserId: user.googleId,
|
googleUserId: user.googleId,
|
||||||
@@ -139,10 +138,10 @@ async function login(req: Request, res: Response) {
|
|||||||
email: user.email,
|
email: user.email,
|
||||||
name: user.name,
|
name: user.name,
|
||||||
},
|
},
|
||||||
config.JWT_TOKEN_SECRET
|
config.JWT_TOKEN_SECRET,
|
||||||
);
|
);
|
||||||
|
|
||||||
res.status(200).json(new LoginResponse(jwtToken, user));
|
res.status(200).json(new LoginResponse(existingUserJwtToken, user));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
res.status(500).json(`Problem with signing user up or logging in`);
|
res.status(500).json(`Problem with signing user up or logging in`);
|
||||||
|
|||||||
Reference in New Issue
Block a user