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();
|
||||
|
||||
// used by specific routes that need to authentication
|
||||
export const authCheck = async (
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
) => {
|
||||
export const authCheck = async (req: Request, res: Response, next: NextFunction) => {
|
||||
try {
|
||||
const { authorization } = req.headers;
|
||||
const { authentication } = req.headers;
|
||||
|
||||
if (!authorization) {
|
||||
throw Error("No provided header");
|
||||
if (!authentication) {
|
||||
throw Error('No provided header');
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
next();
|
||||
} 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
|
||||
router.get('/', authCheck, getUserLines);
|
||||
|
||||
// toggle tune into a line
|
||||
// toggle tune into or off a line and persist
|
||||
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
|
||||
router.get('/dm/:otherUserId', authCheck, getDmByOtherUserId);
|
||||
|
||||
@@ -214,3 +217,21 @@ async function getUserLines(req: Request, res: Response) {
|
||||
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 { 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';
|
||||
import NirvanaResponse from '@nirvana/core/responses/nirvanaResponse';
|
||||
|
||||
const jwt = require("jsonwebtoken");
|
||||
const jwt = require('jsonwebtoken');
|
||||
|
||||
const config = loadConfig();
|
||||
|
||||
@@ -24,20 +25,22 @@ 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);
|
||||
|
||||
router.get('/:userId', authCheck, getOtherUserData);
|
||||
|
||||
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 +52,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 +68,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 +85,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 +105,7 @@ async function login(req: Request, res: Response) {
|
||||
new Date(),
|
||||
userInfo.picture,
|
||||
userInfo.verifiedEmail,
|
||||
userInfo.locale
|
||||
userInfo.locale,
|
||||
);
|
||||
|
||||
// create user if not exists
|
||||
@@ -120,12 +122,12 @@ 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;
|
||||
}
|
||||
@@ -139,7 +141,7 @@ 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));
|
||||
@@ -148,3 +150,26 @@ async function login(req: Request, res: Response) {
|
||||
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 { UserStatus } from "../../core/models/user.model";
|
||||
import axios from "axios";
|
||||
import { collections } from "./database.service";
|
||||
import { ObjectId } from 'mongodb';
|
||||
import { UserStatus } from '../../core/models/user.model';
|
||||
import axios from 'axios';
|
||||
import { collections } from './database.service';
|
||||
|
||||
export class UserService {
|
||||
static async getUserById(userId: string) {
|
||||
@@ -62,11 +62,11 @@ export class UserService {
|
||||
// based on index defined in Mongo atlas
|
||||
const query = {
|
||||
$search: {
|
||||
index: "basic user search",
|
||||
index: 'basic user search',
|
||||
text: {
|
||||
query: searchQuery,
|
||||
path: {
|
||||
wildcard: "*",
|
||||
wildcard: '*',
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -95,13 +95,9 @@ export class UserService {
|
||||
return null;
|
||||
}
|
||||
|
||||
static async getGoogleUserInfoWithAccessToken(
|
||||
accessToken: string
|
||||
): Promise<GoogleUserInfo> {
|
||||
static async getGoogleUserInfoWithAccessToken(accessToken: string): Promise<GoogleUserInfo> {
|
||||
return (
|
||||
await axios.get(
|
||||
`https://www.googleapis.com/oauth2/v1/userinfo?access_token=${accessToken}`
|
||||
)
|
||||
await axios.get(`https://www.googleapis.com/oauth2/v1/userinfo?access_token=${accessToken}`)
|
||||
).data;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user