starting implementation
This commit is contained in:
@@ -1,32 +1,28 @@
|
|||||||
import { NextFunction, Request, Response } from "express";
|
import { NextFunction, Request, Response } from 'express';
|
||||||
|
|
||||||
import { loadConfig } from "../config";
|
import { loadConfig } from '../config';
|
||||||
|
|
||||||
const jwt = require("jsonwebtoken");
|
const jwt = require('jsonwebtoken');
|
||||||
|
|
||||||
const config = loadConfig();
|
const config = loadConfig();
|
||||||
|
|
||||||
// used by specific routes that need to authentication
|
// used by specific routes that need to authentication
|
||||||
export const authCheck = async (
|
export const authCheck = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
req: Request,
|
|
||||||
res: Response,
|
|
||||||
next: NextFunction
|
|
||||||
) => {
|
|
||||||
try {
|
try {
|
||||||
const { authorization } = req.headers;
|
const { authentication } = req.headers;
|
||||||
|
|
||||||
if (!authorization) {
|
if (!authentication) {
|
||||||
throw Error("No provided header");
|
throw Error('No provided header');
|
||||||
}
|
}
|
||||||
|
|
||||||
// verify jwt token with our api secret
|
// verify jwt token with our api secret
|
||||||
var decoded: JwtClaims = jwt.verify(authorization, config.JWT_TOKEN_SECRET);
|
var decoded: JwtClaims = jwt.verify(authentication, config.JWT_TOKEN_SECRET);
|
||||||
|
|
||||||
res.locals.userInfo = decoded;
|
res.locals.userInfo = decoded;
|
||||||
|
|
||||||
next();
|
next();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(401).send("unauthorized");
|
res.status(401).send('unauthorized');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -31,9 +31,12 @@ export default function getLineRoutes() {
|
|||||||
// get all of user's lines
|
// get all of user's lines
|
||||||
router.get('/', authCheck, getUserLines);
|
router.get('/', authCheck, getUserLines);
|
||||||
|
|
||||||
// toggle tune into a line
|
// toggle tune into or off a line and persist
|
||||||
router.post('/:lineId/state', authCheck, updateLineMemberState);
|
router.post('/:lineId/state', authCheck, updateLineMemberState);
|
||||||
|
|
||||||
|
// get all details of a line with user associations and other users
|
||||||
|
router.post('/:lineId', authCheck, getLineDetails);
|
||||||
|
|
||||||
// get conversation between user and other user
|
// get conversation between user and other user
|
||||||
router.get('/dm/:otherUserId', authCheck, getDmByOtherUserId);
|
router.get('/dm/:otherUserId', authCheck, getDmByOtherUserId);
|
||||||
|
|
||||||
@@ -214,3 +217,21 @@ async function getUserLines(req: Request, res: Response) {
|
|||||||
res.status(500).json(error);
|
res.status(500).json(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getLineDetails(req: Request, res: Response) {
|
||||||
|
try {
|
||||||
|
const { lineId } = req.params;
|
||||||
|
|
||||||
|
const userInfo = res.locals.userInfo as JwtClaims;
|
||||||
|
|
||||||
|
// get all the line associations with lineId
|
||||||
|
|
||||||
|
// find the one for me
|
||||||
|
|
||||||
|
// const lineData = new MasterLineData();
|
||||||
|
|
||||||
|
// res.json(new NirvanaResponse<GetUserLinesResponse>(resObj));
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json(error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+55
-30
@@ -1,18 +1,19 @@
|
|||||||
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';
|
||||||
|
import NirvanaResponse from '@nirvana/core/responses/nirvanaResponse';
|
||||||
|
|
||||||
const jwt = require("jsonwebtoken");
|
const jwt = require('jsonwebtoken');
|
||||||
|
|
||||||
const config = loadConfig();
|
const config = loadConfig();
|
||||||
|
|
||||||
@@ -24,20 +25,22 @@ 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);
|
||||||
|
|
||||||
|
router.get('/:userId', authCheck, getOtherUserData);
|
||||||
|
|
||||||
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 +52,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 +68,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 +85,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 +105,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,12 +122,12 @@ 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;
|
||||||
}
|
}
|
||||||
@@ -139,7 +141,7 @@ 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(jwtToken, user));
|
||||||
@@ -148,3 +150,26 @@ async function login(req: Request, res: Response) {
|
|||||||
res.status(500).json(`Problem with signing user up or logging in`);
|
res.status(500).json(`Problem with signing user up or logging in`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param take in userId
|
||||||
|
* return current user object for them
|
||||||
|
*/
|
||||||
|
async function getOtherUserData(req: Request, res: Response) {
|
||||||
|
try {
|
||||||
|
const { userId } = req.params;
|
||||||
|
|
||||||
|
const user = await UserService.getUserById(userId);
|
||||||
|
|
||||||
|
if (!user)
|
||||||
|
return res
|
||||||
|
.status(400)
|
||||||
|
.json(new NirvanaResponse(undefined, new Error('Could not find user!')));
|
||||||
|
|
||||||
|
return res.status(200).json(new NirvanaResponse<User>(user));
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
res.status(500).json(new NirvanaResponse(undefined, error as Error));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { GoogleUserInfo, User } from "@nirvana/core/models";
|
import { GoogleUserInfo, User } from '@nirvana/core/models';
|
||||||
|
|
||||||
import { ObjectId } from "mongodb";
|
import { ObjectId } from 'mongodb';
|
||||||
import { UserStatus } from "../../core/models/user.model";
|
import { UserStatus } from '../../core/models/user.model';
|
||||||
import axios from "axios";
|
import axios from 'axios';
|
||||||
import { collections } from "./database.service";
|
import { collections } from './database.service';
|
||||||
|
|
||||||
export class UserService {
|
export class UserService {
|
||||||
static async getUserById(userId: string) {
|
static async getUserById(userId: string) {
|
||||||
@@ -62,11 +62,11 @@ export class UserService {
|
|||||||
// based on index defined in Mongo atlas
|
// based on index defined in Mongo atlas
|
||||||
const query = {
|
const query = {
|
||||||
$search: {
|
$search: {
|
||||||
index: "basic user search",
|
index: 'basic user search',
|
||||||
text: {
|
text: {
|
||||||
query: searchQuery,
|
query: searchQuery,
|
||||||
path: {
|
path: {
|
||||||
wildcard: "*",
|
wildcard: '*',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -95,13 +95,9 @@ export class UserService {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
static async getGoogleUserInfoWithAccessToken(
|
static async getGoogleUserInfoWithAccessToken(accessToken: string): Promise<GoogleUserInfo> {
|
||||||
accessToken: string
|
|
||||||
): Promise<GoogleUserInfo> {
|
|
||||||
return (
|
return (
|
||||||
await axios.get(
|
await axios.get(`https://www.googleapis.com/oauth2/v1/userinfo?access_token=${accessToken}`)
|
||||||
`https://www.googleapis.com/oauth2/v1/userinfo?access_token=${accessToken}`
|
|
||||||
)
|
|
||||||
).data;
|
).data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user