From e6bcba84acc532eb379775bafc2cab148d91f3b5 Mon Sep 17 00:00:00 2001 From: Ruidy Nemausat Date: Tue, 5 May 2020 14:29:55 +0200 Subject: [PATCH] create user profile after signup if not exists --- Controllers/AppUsersController.cs | 1 + app.db | Bin 106496 -> 106496 bytes .../src/components/Modals/NewTicketModal.tsx | 3 +- client/src/components/SignInSide.tsx | 2 +- client/src/controllers/UserController.tsx | 48 +++++++++++++----- client/src/services/index.ts | 2 +- client/src/services/project.ts | 7 +-- client/src/services/ticket.ts | 6 ++- client/src/services/user.ts | 6 ++- 9 files changed, 52 insertions(+), 23 deletions(-) diff --git a/Controllers/AppUsersController.cs b/Controllers/AppUsersController.cs index 63bedb2..1f254de 100644 --- a/Controllers/AppUsersController.cs +++ b/Controllers/AppUsersController.cs @@ -159,6 +159,7 @@ namespace TicketManager.Controllers var user = new AppUser() { + Id = userDto.Id, FirstName = userDto.FirstName, LastName = userDto.LastName, Presentation = userDto.Presentation, diff --git a/app.db b/app.db index 26f2100ffafc4018b559d832fd0eff425a450532..ff1acb8d9398f4ebca6f8723332b96891920bd75 100644 GIT binary patch delta 286 zcmZoTz}9epZ3Bw|7c;LW1Ft3@JHN|jK>;b=%^J4<7#S5OtJ}-gH(D}tnTyLRoAWw0 z8XFp#8dw@wni`uJnwgkb8krlJ8W;tYW~Nm7rRFA<7AKbELFs~`d?cnrdTwH7j$U$p zu91O}fv$lm5Gfd%SQ(jHnHcMtTbfuJn`V@h6ck(O>*r(`>!s)Cr{|=W7N-_@C+Fvt zq~?`?^y(+->O1;p7@D|e;8 = ({ show, handleClose, allProjects }) => { title: title, description: description, endingDate: new Date(endingDate).toISOString(), - creatorId: "20bf4b2a-7209-4826-96cd-29c2bc937a94", // get current User id + creatorId: getUID(user), projectId: parseInt(projectId), impact: impactID, difficulty: difficultyID, diff --git a/client/src/components/SignInSide.tsx b/client/src/components/SignInSide.tsx index be4bbcc..ef44700 100644 --- a/client/src/components/SignInSide.tsx +++ b/client/src/components/SignInSide.tsx @@ -46,7 +46,7 @@ const useStyles = makeStyles((theme: Theme) => export default function SignInSide() { const classes = useStyles(); - const { isAuthenticated, loginWithRedirect, logout } = useAuth0(); + const { loginWithRedirect } = useAuth0(); return ( diff --git a/client/src/controllers/UserController.tsx b/client/src/controllers/UserController.tsx index bd7d326..e7b7b1a 100644 --- a/client/src/controllers/UserController.tsx +++ b/client/src/controllers/UserController.tsx @@ -7,30 +7,52 @@ import User from "../types/User"; import Preloader from "../components/Preloader"; import { UserService } from "../services"; import { useAuth0 } from "../authentication/auth0"; +import { getUID } from "../authentication/helpers"; const UserController: FC = () => { const [isLoading, setIsLoading] = useState(true); - const [user, setUser] = useState({} as User); + const [account, setAccount] = useState({} as User); const [hasError, setHasError] = useState(false); const [error, setError] = useState(""); const { id } = useParams(); - const { getTokenSilently } = useAuth0(); + const { getTokenSilently, user } = useAuth0(); useEffect(() => { const getUser = async (id: string): Promise => { - try { - const token = await getTokenSilently(); - const Users = new UserService(token); - const response: User = await Users.get(id); + const token = await getTokenSilently(); + const Users = new UserService(token); + let response: User | undefined; + try { + response = await Users.get(id); + } catch (ex) { + if (ex === "Not Found") { + // create user + const { given_name, family_name, email, nickname, picture } = user; + const newUser: User = { + id: getUID(user), + firstName: given_name, + lastName: family_name, + fullName: `${given_name} ${family_name}`, + email, + presentation: nickname, + picture, + phone: "", + creationDate: Date.now().toLocaleString(), + activities: [], + projects: [], + tickets: [], + }; + response = await Users.add(newUser); + } else { + setHasError(true); + setError(ex); + } + } finally { if (response !== undefined) { - setUser(response); + setAccount(response); setIsLoading(false); } - } catch (ex) { - console.error(ex); - setHasError(true); - setError(ex); } }; @@ -40,13 +62,13 @@ const UserController: FC = () => { setHasError(true); setError("Bad Request"); } - }, [id, getTokenSilently]); + }, [id, getTokenSilently, user]); if (hasError) { return ; } - const viewModel = new UserVM(user); + const viewModel = new UserVM(account); return isLoading ? : ; }; diff --git a/client/src/services/index.ts b/client/src/services/index.ts index 7266453..40ca183 100644 --- a/client/src/services/index.ts +++ b/client/src/services/index.ts @@ -5,7 +5,7 @@ import UserService from "./user"; export default interface IService { all(): Promise; get(id: string): Promise; - add(item: any): Promise; + add(item: any): Promise; update(id: string, item: T): Promise; delete(id: string): Promise; } diff --git a/client/src/services/project.ts b/client/src/services/project.ts index d745b35..22ff989 100644 --- a/client/src/services/project.ts +++ b/client/src/services/project.ts @@ -1,7 +1,6 @@ import IService from "."; import Project from "../types/Project"; import HttpHandler from "./http"; -import User from "../types/User"; interface NewProject { title: string; @@ -26,8 +25,10 @@ export default class ProjectService implements IService { return body ?? ({} as Project); }; - add = async (item: NewProject): Promise => { - await this.http.post(this.path, item, this.key); + add = async (item: NewProject): Promise => { + const response = await this.http.post(this.path, item, this.key); + const body = response.parsedBody; + return body ?? ({} as Project); }; update(id: string, item: Project): Promise { diff --git a/client/src/services/ticket.ts b/client/src/services/ticket.ts index d97e514..de9eb4e 100644 --- a/client/src/services/ticket.ts +++ b/client/src/services/ticket.ts @@ -30,8 +30,10 @@ export default class TicketService implements IService { return body ?? ({} as Ticket); }; - add = async (item: NewTicket): Promise => { - await this.http.post(this.path, item, this.key); + add = async (item: NewTicket): Promise => { + const response = await this.http.post(this.path, item, this.key); + const body = response.parsedBody; + return body ?? ({} as Ticket); }; update(id: string, item: Ticket): Promise { diff --git a/client/src/services/user.ts b/client/src/services/user.ts index 5eec469..0a81b6b 100644 --- a/client/src/services/user.ts +++ b/client/src/services/user.ts @@ -19,8 +19,10 @@ export default class UserService implements IService { return body ?? ({} as User); }; - add = async (item: User): Promise => { - await this.http.post(this.path, item, this.key); + add = async (item: User): Promise => { + const response = await this.http.post(this.path, item, this.key); + const body = response.parsedBody; + return body ?? ({} as User); }; update = async (id: string, item: User): Promise => {