cleaning up api and adding in conversation create route
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import express, { NextFunction, Request, Response } from 'express';
|
||||
|
||||
import Conversation from '@nirvana/core/models/conversation.model';
|
||||
import ConversationService from '../services/conversation.service';
|
||||
import CreateConversationRequest from '@nirvana/core/requests/CreateConversationRequest.request';
|
||||
import CreateConversationResponse from '@nirvana/core/responses/CreateConversationResponse.response';
|
||||
import NirvanaResponse from '@nirvana/core/responses/nirvanaResponse';
|
||||
import { authCheck } from '../middleware/auth';
|
||||
|
||||
export default function getConversationRoutes() {
|
||||
const router = express.Router();
|
||||
|
||||
router.use(express.json());
|
||||
|
||||
// get a conversation
|
||||
router.get('/:conversationId', authCheck);
|
||||
|
||||
// get all conversations that I am in
|
||||
router.get('/conversations', authCheck);
|
||||
|
||||
// get a one on one conversation based on other user Id
|
||||
|
||||
// get a conversation's content
|
||||
|
||||
// create a conversation
|
||||
router.post('/', authCheck, createConversation);
|
||||
|
||||
return router;
|
||||
}
|
||||
|
||||
const createConversation = async (req: Request, res: Response, next: NextFunction) => {
|
||||
const createRequest = req.body as CreateConversationRequest;
|
||||
|
||||
next(Error('test this shit'));
|
||||
|
||||
// console.log(createRequest.conversation);
|
||||
|
||||
// const insertResult = await ConversationService.createConversation(createRequest.conversation);
|
||||
|
||||
// if (!insertResult?.acknowledged) {
|
||||
// throw Error('nothing inserted');
|
||||
// }
|
||||
|
||||
// return res.json(new CreateConversationResponse(insertResult.insertedId));
|
||||
};
|
||||
+75
-86
@@ -1,6 +1,6 @@
|
||||
import { JwtClaims, authCheck } from '../middleware/auth';
|
||||
import User, { GoogleUserInfo, UserStatus } from '@nirvana/core/models/user.model';
|
||||
import express, { Request, Response } from 'express';
|
||||
import express, { NextFunction, Request, Response } from 'express';
|
||||
|
||||
import LoginResponse from '../../core/responses/login.response';
|
||||
import { OAuth2Client } from 'google-auth-library';
|
||||
@@ -29,116 +29,105 @@ export default function getUserRoutes() {
|
||||
}
|
||||
|
||||
async function handleAuthCheck(req: Request, res: Response) {
|
||||
try {
|
||||
res.status(200).json('You are good to go!');
|
||||
} catch (error) {
|
||||
res.status(401).json('Unauthorized');
|
||||
}
|
||||
res.status(200).json('You are good to go!');
|
||||
}
|
||||
|
||||
async function getUserDetails(req: Request, res: Response) {
|
||||
try {
|
||||
const userInfo = res.locals.userInfo as JwtClaims;
|
||||
async function getUserDetails(req: Request, res: Response, next: NextFunction) {
|
||||
const userInfo = res.locals.userInfo as JwtClaims;
|
||||
|
||||
const user = await UserService.getUserById(userInfo.userId);
|
||||
const user = await UserService.getUserById(userInfo.userId);
|
||||
|
||||
user
|
||||
? res.status(200).json(new UserDetailsResponse(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`);
|
||||
if (user) {
|
||||
return res.status(200).json(new UserDetailsResponse(user));
|
||||
}
|
||||
|
||||
res.status(404);
|
||||
next(Error('No user found'));
|
||||
}
|
||||
|
||||
/** Create user if doesn't exist
|
||||
* Returns jwt token for client and user details
|
||||
*/
|
||||
async function login(req: Request, res: Response) {
|
||||
async function login(req: Request, res: Response, next: NextFunction) {
|
||||
// passed in accesstoken no matter what
|
||||
const { access_token, id_token } = req.query;
|
||||
|
||||
try {
|
||||
const ticket = await client.verifyIdToken({
|
||||
idToken: (id_token as string) ?? '',
|
||||
audience: environmentVariables.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;
|
||||
const ticket = await client.verifyIdToken({
|
||||
idToken: (id_token as string) ?? '',
|
||||
audience: environmentVariables.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');
|
||||
return;
|
||||
if (!googleUserId || !email) {
|
||||
res.status(401);
|
||||
return next(Error('no google account found'));
|
||||
}
|
||||
|
||||
// return user details if it passed auth middleware
|
||||
const user = await UserService.getUserByEmail(email);
|
||||
|
||||
// if no user found, then go ahead and create user
|
||||
if (!user) {
|
||||
if (!access_token) {
|
||||
res.status(401);
|
||||
return next(Error('No access token provided'));
|
||||
}
|
||||
|
||||
// return user details if it passed auth middleware
|
||||
const user = await UserService.getUserByEmail(email);
|
||||
// get google user info from access token
|
||||
const userInfo: GoogleUserInfo = await UserService.getGoogleUserInfoWithAccessToken(
|
||||
access_token as string,
|
||||
);
|
||||
|
||||
// if no user found, then go ahead and create user
|
||||
if (!user) {
|
||||
if (!access_token) {
|
||||
res.status(400).json('No access token provided');
|
||||
return;
|
||||
}
|
||||
// create initial user model object
|
||||
|
||||
// get google user info from access token
|
||||
const userInfo: GoogleUserInfo = await UserService.getGoogleUserInfoWithAccessToken(
|
||||
access_token as string,
|
||||
);
|
||||
const newUser = new User(
|
||||
googleUserId,
|
||||
userInfo.email,
|
||||
userInfo.name,
|
||||
userInfo.given_name,
|
||||
userInfo.family_name,
|
||||
new Date(),
|
||||
userInfo.picture,
|
||||
userInfo.verifiedEmail,
|
||||
userInfo.locale,
|
||||
);
|
||||
|
||||
// create initial user model object
|
||||
// create user if not exists
|
||||
const insertResult = await UserService.createUserIfNotExists(newUser);
|
||||
|
||||
const newUser = new User(
|
||||
googleUserId,
|
||||
userInfo.email,
|
||||
userInfo.name,
|
||||
userInfo.given_name,
|
||||
userInfo.family_name,
|
||||
new Date(),
|
||||
userInfo.picture,
|
||||
userInfo.verifiedEmail,
|
||||
userInfo.locale,
|
||||
);
|
||||
newUser._id = insertResult?.insertedId;
|
||||
|
||||
// create user if not exists
|
||||
const insertResult = await UserService.createUserIfNotExists(newUser);
|
||||
|
||||
newUser._id = insertResult?.insertedId;
|
||||
|
||||
// create jwt token with new user info
|
||||
const jwtToken = jwt.sign(
|
||||
{
|
||||
userId: newUser._id,
|
||||
googleUserId: newUser.googleId,
|
||||
picture: newUser.picture,
|
||||
email: newUser.email,
|
||||
name: newUser.name,
|
||||
},
|
||||
environmentVariables.JWT_TOKEN_SECRET,
|
||||
);
|
||||
|
||||
insertResult
|
||||
? res.status(200).json(new LoginResponse(jwtToken, newUser))
|
||||
: res.status(500).json('Failed to create account, already exists');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// create jwt token with existing user info
|
||||
const existingUserJwtToken = jwt.sign(
|
||||
// create jwt token with new user info
|
||||
const jwtToken = jwt.sign(
|
||||
{
|
||||
userId: user._id,
|
||||
googleUserId: user.googleId,
|
||||
picture: user.picture,
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
userId: newUser._id,
|
||||
googleUserId: newUser.googleId,
|
||||
picture: newUser.picture,
|
||||
email: newUser.email,
|
||||
name: newUser.name,
|
||||
},
|
||||
environmentVariables.JWT_TOKEN_SECRET,
|
||||
);
|
||||
|
||||
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`);
|
||||
if (insertResult) {
|
||||
return res.status(200).json(new LoginResponse(jwtToken, newUser));
|
||||
}
|
||||
|
||||
return next(Error('Failed to create account, already exists'));
|
||||
}
|
||||
|
||||
// create jwt token with existing user info
|
||||
const existingUserJwtToken = jwt.sign(
|
||||
{
|
||||
userId: user._id,
|
||||
googleUserId: user.googleId,
|
||||
picture: user.picture,
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
},
|
||||
environmentVariables.JWT_TOKEN_SECRET,
|
||||
);
|
||||
|
||||
return res.status(200).json(new LoginResponse(existingUserJwtToken, user));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user