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