adding in proper creation of user

This commit is contained in:
talksik
2022-03-18 17:34:51 -04:00
parent 0a126900b9
commit b9d38a6484
9 changed files with 113 additions and 52 deletions
+2
View File
@@ -24,6 +24,8 @@ export const authCheck = async (
const email = ticket.getPayload()?.email;
// used in subsequent handlers
// todo: have to get our database id for the user instead of google's id
res.locals.userId = userId;
res.locals.email = email;
+57
View File
@@ -0,0 +1,57 @@
import express, { Application, Request, Response } from "express";
import { ObjectId } from "mongodb";
import Relationship from "@nirvana/core/models/relationship.model";
import { authCheck } from "../middleware/auth";
import { collections } from "../services/database.service";
export default function getContactsRoutes() {
const router = express.Router();
router.use(express.json());
// see all of my contacts
router.get("/", authCheck, getAllContacts);
// send a friend request
router.post("/:userId", authCheck, addContact);
return router;
}
async function getAllContacts(req: Request, res: Response) {
try {
const { email } = res.locals;
if (!email) {
res.status(400).send("No email of user!");
return;
}
// todo: fetch all of my contacts
} catch (error) {
console.log(error);
res.status(500).send(`something went wrong`);
}
}
async function addContact(req: Request, res: Response) {
try {
// get the userId of the person to add as a friend/contact
const { userId } = req.params;
if (!userId) {
res.status(400).send("No contact id provided!");
return;
}
// validations
// make sure that we are not adding a friend that already has added us
// add a many to many table of friends
// const newRelationship = new Relationship();
} catch (error) {
console.log(error);
res.status(500).send(`something went wrong`);
}
}
+13 -47
View File
@@ -1,8 +1,10 @@
import { GoogleUserInfo, User } from "@nirvana/core/models";
import express, { Application, Request, Response } from "express";
import { ObjectID } from "bson";
import { ObjectId } from "mongodb";
import { UserService } from "../services/user.service";
import { UserStatus } from "../../core/models/user.model";
import { authCheck } from "../middleware/auth";
import { collections } from "../services/database.service";
@@ -14,8 +16,6 @@ export default function getUserRoutes() {
// get user details based on id token
router.get("/", authCheck, getUserDetails);
router.post("/create", createUser);
return router;
}
@@ -25,15 +25,18 @@ export default function getUserRoutes() {
*/
async function getUserDetails(req: Request, res: Response) {
const email: string = res.locals.email;
const userId: string = res.locals.userId;
// passed in accesstoken no matter what
const { access_token } = req.query;
console.log(`getting data for ${email}`);
console.log(res.locals);
console.log(`getting data for ${userId}`);
try {
// return user details if it passed auth middleware
const user = await UserService.getUserByEmail(email);
const user = await UserService.getUserById(userId);
// if no user found, then go ahead and create user
if (!user) {
@@ -50,18 +53,21 @@ async function getUserDetails(req: Request, res: Response) {
// create initial user model object
const newUser = new User(
userId,
userInfo.email,
userInfo.verifiedEmail,
userInfo.name,
userInfo.given_name,
userInfo.family_name,
userInfo.picture,
userInfo.locale
userInfo.locale,
new Date(),
UserStatus.ONLINE,
new Date()
);
// create user if not exists
const insertResult = await UserService.createUserIfNotExists(newUser);
newUser._id = insertResult?.insertedId;
insertResult
? res.status(200).send(newUser)
@@ -72,48 +78,8 @@ async function getUserDetails(req: Request, res: Response) {
// otherwise, just return the user details
res.status(200).send(user);
} catch (error) {
res
.status(404)
.send(`unable to find a matching document with email: ${email}`);
}
}
/** DEPRECATED...USING THE SAME SIGN IN ROUTE TO CREATE */
async function createUser(req: Request, res: Response) {
try {
const { access_token } = req.query;
if (!access_token) {
res.status(400).send("No access token provided");
return;
}
// get google user info from access token
const userInfo: GoogleUserInfo =
await UserService.getGoogleUserInfoWithAccessToken(
access_token as string
);
// create initial user model object
const newUser = new User(
userInfo.email,
userInfo.verifiedEmail,
userInfo.name,
userInfo.given_name,
userInfo.family_name,
userInfo.picture,
userInfo.locale
);
// create user if not exists
const insertResult = await UserService.createUserIfNotExists(newUser);
insertResult
? res.status(200).send("User created")
: res.status(500).send("Failed to create account, already exists");
} catch (error) {
console.log(error);
res.status(500).send("Problem in creating user");
res.status(500).send(`Problem with signing user up or logging in`);
}
}
+2 -2
View File
@@ -6,7 +6,7 @@ import { collections } from "./database.service";
export class UserService {
static async getUserById(userId: string) {
const query = { _id: new ObjectId(userId) };
const query = { googleId: userId };
const res = await collections.users?.findOne(query);
@@ -58,7 +58,7 @@ export class UserService {
}
static async createUserIfNotExists(newUser: User) {
const exists = (await collections.users?.findOne({ email: newUser.email }))
const exists = (await collections.users?.findOne({ id: newUser.googleId }))
?._id;
if (!exists) {