diff --git a/packages/api/middleware/auth.ts b/packages/api/middleware/auth.ts index 2fce164..68a6dfc 100644 --- a/packages/api/middleware/auth.ts +++ b/packages/api/middleware/auth.ts @@ -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; diff --git a/packages/api/routes/contacts.ts b/packages/api/routes/contacts.ts new file mode 100644 index 0000000..fe735cb --- /dev/null +++ b/packages/api/routes/contacts.ts @@ -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`); + } +} diff --git a/packages/api/routes/user.ts b/packages/api/routes/user.ts index e8123aa..c0dd689 100644 --- a/packages/api/routes/user.ts +++ b/packages/api/routes/user.ts @@ -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`); } } diff --git a/packages/api/services/user.service.ts b/packages/api/services/user.service.ts index 747280c..e153028 100644 --- a/packages/api/services/user.service.ts +++ b/packages/api/services/user.service.ts @@ -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) { diff --git a/packages/core/models/relationship.model.ts b/packages/core/models/relationship.model.ts new file mode 100644 index 0000000..8864359 --- /dev/null +++ b/packages/core/models/relationship.model.ts @@ -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", +} diff --git a/packages/core/models/user.model.ts b/packages/core/models/user.model.ts index 1dc30fb..c8f4631 100644 --- a/packages/core/models/user.model.ts +++ b/packages/core/models/user.model.ts @@ -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", +} diff --git a/packages/desktop/src/components/ProtectedRoute/index.tsx b/packages/desktop/src/components/ProtectedRoute/index.tsx index 97c94a8..9881511 100644 --- a/packages/desktop/src/components/ProtectedRoute/index.tsx +++ b/packages/desktop/src/components/ProtectedRoute/index.tsx @@ -9,7 +9,6 @@ export default function ProtectedRoute({ children?: React.ReactNode; }) { const { data, isLoading, isError, refetch } = useGetUserDetails(); - console.log(data); if (isLoading) return ( diff --git a/packages/desktop/src/index.html b/packages/desktop/src/index.html index 370a0f9..535f501 100644 --- a/packages/desktop/src/index.html +++ b/packages/desktop/src/index.html @@ -2,7 +2,7 @@ - Hello World! + Nirvana
diff --git a/packages/desktop/src/pages/Login/index.tsx b/packages/desktop/src/pages/Login/index.tsx index c135217..2bc03a7 100644 --- a/packages/desktop/src/pages/Login/index.tsx +++ b/packages/desktop/src/pages/Login/index.tsx @@ -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)