adding in proper creation of user
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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
@@ -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`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import { ObjectId } from "mongodb";
|
||||
|
||||
// some sort of contact or friend model
|
||||
|
||||
export default class Relationship {
|
||||
constructor(
|
||||
public senderUserId: string,
|
||||
public receiverUserId: string,
|
||||
public state: RelationshipState,
|
||||
public createdDate: Date = new Date(),
|
||||
public lastUpdatedDate: Date = new Date(),
|
||||
|
||||
public _id: ObjectId = new ObjectId()
|
||||
) {}
|
||||
}
|
||||
|
||||
export enum RelationshipState {
|
||||
PENDING = "PENDING",
|
||||
ACTIVE = "ACTIVE",
|
||||
BLOCKED = "BLOCKED",
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import { ObjectId } from "mongodb";
|
||||
|
||||
export class User {
|
||||
constructor(
|
||||
public googleId: string, // our Google id that every google user has unique that we are going to use for now
|
||||
public email: string,
|
||||
public verifiedEmail: boolean,
|
||||
public name: string,
|
||||
@@ -9,6 +10,18 @@ export class User {
|
||||
public family_name: string,
|
||||
public picture: string,
|
||||
public locale: string,
|
||||
public _id?: ObjectId // additional properties specific to our users collection
|
||||
|
||||
// additional properties specific to our users collection
|
||||
|
||||
public createdDate: Date,
|
||||
public status: UserStatus,
|
||||
public lastUpdatedDate?: Date,
|
||||
public _id?: ObjectId
|
||||
) {}
|
||||
}
|
||||
|
||||
export enum UserStatus {
|
||||
ONLINE = "ONLINE",
|
||||
OFFLINE = "OFFLINE",
|
||||
FLOW_STATE = "FLOW_STATE",
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ export default function ProtectedRoute({
|
||||
children?: React.ReactNode;
|
||||
}) {
|
||||
const { data, isLoading, isError, refetch } = useGetUserDetails();
|
||||
console.log(data);
|
||||
|
||||
if (isLoading)
|
||||
return (
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<title>Hello World!</title>
|
||||
<title>Nirvana</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
|
||||
@@ -37,6 +37,9 @@ export default function Login({ onReady }: { onReady: Function }) {
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
// hack for now log out in this case
|
||||
logOut();
|
||||
|
||||
// see if we have tokens in localstorage in which case we can continue on
|
||||
window.electronAPI.store
|
||||
.get(STORE_ITEMS.AUTH_TOKENS)
|
||||
|
||||
Reference in New Issue
Block a user