diff --git a/.gitignore b/.gitignore index fc44559..648e27a 100644 --- a/.gitignore +++ b/.gitignore @@ -39,4 +39,7 @@ testem.log Thumbs.db # api keys -.env \ No newline at end of file +.env + +# service account +wol-planner-7e216-cf2ab387f459.json \ No newline at end of file diff --git a/apps/frontend/src/app/api-client/apiModules/users.ts b/apps/frontend/src/app/api-client/apiModules/users.ts index d863102..bc7e585 100644 --- a/apps/frontend/src/app/api-client/apiModules/users.ts +++ b/apps/frontend/src/app/api-client/apiModules/users.ts @@ -76,3 +76,9 @@ export const allFilesForUser = async (path: string) => { return allItems } + +export const signUpUser = async (data: any) => { + const userData = await apiClientWithAuth.post(apiEndpoints.users + "/signup", data) + console.log(data) + return userData.data +} \ No newline at end of file diff --git a/apps/frontend/src/app/firebase/firebase.ts b/apps/frontend/src/app/firebase/firebase.ts index fded8fb..dd4014b 100644 --- a/apps/frontend/src/app/firebase/firebase.ts +++ b/apps/frontend/src/app/firebase/firebase.ts @@ -2,6 +2,7 @@ import { initializeApp } from "@firebase/app"; import { getAnalytics } from "firebase/analytics"; import { getStorage } from "@firebase/storage"; import { getAuth, GoogleAuthProvider } from "firebase/auth" +import { getFirestore, collection, getDocs } from "firebase/firestore"; export const settings = { project_id: 'wol-planner-7e216', @@ -15,12 +16,31 @@ const firebaseConfig = { storageBucket: process.env.NX_STORAGE_BUCKET, messagingSenderId: process.env.NX_MESSAGING_SENDER_ID, appId: "1:1092456040603:web:83a5c56785f71e0ff81e3e", - measurementId: process.env.NX_MEASUREMENT_ID + measurementId: process.env.NX_MEASUREMENT_ID }; - +console.log(firebaseConfig) // Initialize Firebase const app = initializeApp(firebaseConfig); const analytics = getAnalytics(app); export const storage = getStorage(app); export const auth = getAuth(app) -export const googleProvider = new GoogleAuthProvider() \ No newline at end of file +export const googleProvider = new GoogleAuthProvider() + +// init services +// const db = getFirestore() + +// collection reference +// const colRef = collection(db, "user") + +//get collection data +// getDocs(colRef) +// .then((snapshot) => { +// let user1 = [] +// snapshot.docs.forEach((doc) => { +// user1.push({...doc.data(), id: doc.id}) +// }) +// console.log(user1) +// }) +// .catch(err => { +// console.log(err.message) +// }) diff --git a/apps/frontend/src/app/views/login/Login.tsx b/apps/frontend/src/app/views/login/Login.tsx index 728fa7d..3905343 100644 --- a/apps/frontend/src/app/views/login/Login.tsx +++ b/apps/frontend/src/app/views/login/Login.tsx @@ -3,8 +3,8 @@ import React, { ChangeEvent, useEffect } from 'react'; import { UserContext } from '../../app'; import { useGoogleLogin } from '@react-oauth/google'; -// import axios from 'axios'; -// import { createUserData } from '../../api-client/apiModules/users'; +import axios from 'axios'; +import { createUserData } from '../../api-client/apiModules/users'; import { AuthLayout } from '../../components/authlayout/AuthLayout'; import { Link, useNavigate } from 'react-router-dom'; import googleGImage from "../../images/logos/googleGImage.svg" @@ -27,38 +27,39 @@ export const Login = () => { const login = useGoogleLogin({ onSuccess: (codeResponse) => ( - setSignIn(codeResponse), - navigate("/dashboard") + setSignIn(codeResponse) + // window.location.replace(localhost:4200/dashboard) //`${window.location.origin}/whatever` ), onError: (error) => console.log('Login Failed:', error) }); - // useEffect( - // () => { - // const signInUser = async () => { - // if (signIn) { - // axios - // .get(`https://www.googleapis.com/oauth2/v1/userinfo?access_token=${signIn.access_token}`, { - // headers: { - // Authorization: `Bearer ${signIn.access_token}`, - // Accept: 'application/json' - // } - // }) - // .then(async (res) => { - // const userData = await createUserData({name: res.data.name, email: res.data.email, picture: res.data.picture}); - // setUser(userData); - // console.log("useEffect signed in user", userData) - // localStorage.setItem('user', JSON.stringify(userData)); - // }) - // .catch((err) => console.log(err)); - // } - // } - // signInUser(); - // }, - // [ signIn ] - // ); + useEffect( + () => { + const signInUser = async () => { + if (signIn) { + axios + .get(`https://www.googleapis.com/oauth2/v1/userinfo?access_token=${signIn.access_token}`, { + headers: { + Authorization: `Bearer ${signIn.access_token}`, + Accept: 'application/json' + } + }) + .then(async (res) => { + const userData = await createUserData({name: res.data.name, email: res.data.email, picture: res.data.picture}); + setUser(userData); + console.log("useEffect signed in user", userData) + localStorage.setItem('user', JSON.stringify(userData)) + navigate("/dashboard") + }) + .catch((err) => console.log(err)); + } + } + signInUser(); + }, + [ signIn ] + ); useEffect(() => { const emailRegex = new RegExp(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/); if (!emailRegex.test(email)) { diff --git a/apps/frontend/src/app/views/signup/SignUp.tsx b/apps/frontend/src/app/views/signup/SignUp.tsx index 9ead548..366c638 100644 --- a/apps/frontend/src/app/views/signup/SignUp.tsx +++ b/apps/frontend/src/app/views/signup/SignUp.tsx @@ -8,6 +8,8 @@ import { createUserWithEmailAndPassword } from "firebase/auth" import { auth } from "../../firebase/firebase" import { useNavigate } from 'react-router-dom'; import { AuthLayout } from '../../components/authlayout/AuthLayout'; +import { signUpUser, getUsersFromSearch } from "../../api-client/apiModules/users" + export const SignUp = () => { @@ -35,26 +37,38 @@ export const SignUp = () => { const emailRegex = new RegExp(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/); if (!emailRegex.test(email)) { setEmailError(true) + setIsDisabled(true) } else { setEmailError(false) + setIsDisabled(false) } }) - const createUser = async () => { - if(firstName.length < 1 || lastName.length < 1 || username.length < 1 || emailError === true || password.length < 7) { + // const createUser = async () => { + // if(firstName.length < 1 || lastName.length < 1 || username.length < 1 || emailError === true || password.length < 7) { + // setInputError(true) + // } + // else { + // try { + // await createUserWithEmailAndPassword(auth, email, password) + // navigate("/namepage") + // } + // catch(err: any) { + // console.error(err.message) + // setInputError(true) + // // setAuthError(err.message) + // } + // } + // } + const signUp = async () => { + try { + await signUpUser({email, password, firstName, lastName, username}) + // navigate("/dashboard") + } + catch(err: any) { + console.log(err.message) setInputError(true) - } - else { - try { - await createUserWithEmailAndPassword(auth, email, password) - navigate("/namepage") - } - catch(err: any) { - console.error(err.message) - setInputError(true) - // setAuthError(err.message) - } - } - } + } + } return ( @@ -75,46 +89,46 @@ export const SignUp = () => {

* Indicates Required

-

First Name

+

*First Name

- setFirstName(e.target.value)}/> + setFirstName(e.target.value)}/>
-

Last Name

+

*Last Name

- setLastName(e.target.value)}/> + setLastName(e.target.value)}/>
-

Username

+

*Username

- setUsername(e.target.value)}/> + setUsername(e.target.value)}/>
-

Email

+

*Email

- setEmail(e.target.value)}/> + setEmail(e.target.value)}/>

Please enter a valid email address

-

Password

+

*Password

- setPassword(e.target.value)}/> + setPassword(e.target.value)}/>
+
+

Password must be at least 8 characters

+
-
-
-

Password must be at least 8 characters

-
) diff --git a/apps/frontend/src/app/views/signup/signup.css b/apps/frontend/src/app/views/signup/signup.css index d09c686..2f731e9 100644 --- a/apps/frontend/src/app/views/signup/signup.css +++ b/apps/frontend/src/app/views/signup/signup.css @@ -27,6 +27,7 @@ width: 320px; justify-content: center; align-items: center; + border: none; } .signupFirstNameInputContainer { display: flex; @@ -291,12 +292,13 @@ } .signupLayout { display: grid; - grid-template-rows: 40px 147px 98px 76px 100px 76px 96px 110px; + grid-template-rows: 40px 97px 48px 84px 84px 84px 80px 16px 80px 16px 76px; } .signupIndicatesRequiredContainer { display: flex; justify-content: center; align-items: center; + padding-top: 24px; } .signupIndicatesRequired { color: #F00; @@ -308,7 +310,7 @@ line-height: normal; } .signupEmailErrorContainer { - + padding-left: 6px; } .signupEmailError { color: var(--Primary-Red, #F00); @@ -320,7 +322,7 @@ line-height: normal; } .signupPasswordErrorContainer { - + padding-left: 6px; } .signupPasswordError { color: #F00; @@ -330,4 +332,15 @@ font-style: italic; font-weight: 400; line-height: normal; +} +.signupAsterisk { + font-family: Arial; + font-size: 14px; + font-style: italic; + font-weight: 400; + line-height: 16px; + letter-spacing: 0px; + text-align: left; + color: #ff0000; + padding-right: 4px; } \ No newline at end of file diff --git a/apps/server/src/app/users/users.controller.ts b/apps/server/src/app/users/users.controller.ts index 5271ed3..d77732d 100644 --- a/apps/server/src/app/users/users.controller.ts +++ b/apps/server/src/app/users/users.controller.ts @@ -15,6 +15,10 @@ export class UsersController { signInUser(@Body() body: any) { return this.usersService.signInUser(body); } + @Post("/signup") + signUpUser(@Body() body: any) { + return this.usersService.signUpUser(body); + } @Put() updateUser(@Body() body: any) { diff --git a/apps/server/src/app/users/users.service.ts b/apps/server/src/app/users/users.service.ts index bf1e202..6fd195f 100644 --- a/apps/server/src/app/users/users.service.ts +++ b/apps/server/src/app/users/users.service.ts @@ -13,6 +13,7 @@ export class UsersService { } async signInUser(body): Promise { + console.log(body, "*******") try{ const user = await getDataByField({ collection: 'users', @@ -28,7 +29,45 @@ export class UsersService { body['userType'] = 'Reader' const nameArr = body['name'].split(' ') body['firstName'] = nameArr[0] - body['lastName'] = nameArr[1] + body['lastName'] = nameArr[1] || "" + body['roles'] = [] + body['active'] = true + + const userIdObj = await createData({ + collection: 'users', + params: body + }) + + body['id'] = userIdObj.id + return body + } catch (error) { + console.log(error) + return error + } + } + + async signUpUser(body): Promise { + console.log(body, "======") + try{ + const user = await getDataByField({ + collection: 'users', + field: 'email', + matches: body['email'] + }) + + if(user.length > 0) { + console.log("user already exists") + return user[0] + } + + body['joinDate'] = new Date() + body['userType'] = 'Reader' + // const nameArr = body['name'].split(' ') + // body['email'] = nameArr[0] + // body['password'] = nameArr[1] + // body['firstName'] = nameArr[2] + // body['lastName'] = nameArr[3] + // body['username'] = nameArr[4] body['roles'] = [] body['active'] = true diff --git a/libs/firebase/src/lib/firebase.ts b/libs/firebase/src/lib/firebase.ts index 59555cc..7b79327 100644 --- a/libs/firebase/src/lib/firebase.ts +++ b/libs/firebase/src/lib/firebase.ts @@ -1,4 +1,9 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ + +// connection to firebase +// provides credentials, and functions to manipulate the data in the database + + import * as admin from 'firebase-admin'; import { initializeApp, getApps } from 'firebase-admin/app'; import { getDatabase } from 'firebase-admin/database'; @@ -197,8 +202,8 @@ export const getDataByField = async ( }; interface FindOneInterface { - collection: string; - field: string; + collection: string; + field: string; matches: string; }