create user profile after signup if not exists

This commit is contained in:
Ruidy Nemausat 2020-05-05 14:29:55 +02:00
parent f415e0626a
commit e6bcba84ac
9 changed files with 52 additions and 23 deletions

View file

@ -159,6 +159,7 @@ namespace TicketManager.Controllers
var user = new AppUser() var user = new AppUser()
{ {
Id = userDto.Id,
FirstName = userDto.FirstName, FirstName = userDto.FirstName,
LastName = userDto.LastName, LastName = userDto.LastName,
Presentation = userDto.Presentation, Presentation = userDto.Presentation,

BIN
app.db

Binary file not shown.

View file

@ -14,6 +14,7 @@ import Impact from "../../types/enums/impact";
import Difficulty from "../../types/enums/difficulty"; import Difficulty from "../../types/enums/difficulty";
import { TicketService } from "../../services"; import { TicketService } from "../../services";
import { useAuth0 } from "../../authentication/auth0"; import { useAuth0 } from "../../authentication/auth0";
import { getUID } from "../../authentication/helpers";
interface IProps { interface IProps {
show: boolean; show: boolean;
@ -46,7 +47,7 @@ const NewTicketModal: FC<IProps> = ({ show, handleClose, allProjects }) => {
title: title, title: title,
description: description, description: description,
endingDate: new Date(endingDate).toISOString(), endingDate: new Date(endingDate).toISOString(),
creatorId: "20bf4b2a-7209-4826-96cd-29c2bc937a94", // get current User id creatorId: getUID(user),
projectId: parseInt(projectId), projectId: parseInt(projectId),
impact: impactID, impact: impactID,
difficulty: difficultyID, difficulty: difficultyID,

View file

@ -46,7 +46,7 @@ const useStyles = makeStyles((theme: Theme) =>
export default function SignInSide() { export default function SignInSide() {
const classes = useStyles(); const classes = useStyles();
const { isAuthenticated, loginWithRedirect, logout } = useAuth0(); const { loginWithRedirect } = useAuth0();
return ( return (
<Grid container component="main" className={classes.root}> <Grid container component="main" className={classes.root}>

View file

@ -7,31 +7,53 @@ import User from "../types/User";
import Preloader from "../components/Preloader"; import Preloader from "../components/Preloader";
import { UserService } from "../services"; import { UserService } from "../services";
import { useAuth0 } from "../authentication/auth0"; import { useAuth0 } from "../authentication/auth0";
import { getUID } from "../authentication/helpers";
const UserController: FC = () => { const UserController: FC = () => {
const [isLoading, setIsLoading] = useState(true); const [isLoading, setIsLoading] = useState(true);
const [user, setUser] = useState<User>({} as User); const [account, setAccount] = useState<User>({} as User);
const [hasError, setHasError] = useState(false); const [hasError, setHasError] = useState(false);
const [error, setError] = useState(""); const [error, setError] = useState("");
const { id } = useParams(); const { id } = useParams();
const { getTokenSilently } = useAuth0(); const { getTokenSilently, user } = useAuth0();
useEffect(() => { useEffect(() => {
const getUser = async (id: string): Promise<void> => { const getUser = async (id: string): Promise<void> => {
try {
const token = await getTokenSilently(); const token = await getTokenSilently();
const Users = new UserService(token); const Users = new UserService(token);
const response: User = await Users.get(id); let response: User | undefined;
if (response !== undefined) { try {
setUser(response); response = await Users.get(id);
setIsLoading(false);
}
} catch (ex) { } catch (ex) {
console.error(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); setHasError(true);
setError(ex); setError(ex);
} }
} finally {
if (response !== undefined) {
setAccount(response);
setIsLoading(false);
}
}
}; };
if (id !== undefined) { if (id !== undefined) {
@ -40,13 +62,13 @@ const UserController: FC = () => {
setHasError(true); setHasError(true);
setError("Bad Request"); setError("Bad Request");
} }
}, [id, getTokenSilently]); }, [id, getTokenSilently, user]);
if (hasError) { if (hasError) {
return <ErrorController error={error} />; return <ErrorController error={error} />;
} }
const viewModel = new UserVM(user); const viewModel = new UserVM(account);
return isLoading ? <Preloader /> : <UserPage viewModel={viewModel} />; return isLoading ? <Preloader /> : <UserPage viewModel={viewModel} />;
}; };

View file

@ -5,7 +5,7 @@ import UserService from "./user";
export default interface IService<T> { export default interface IService<T> {
all(): Promise<T[]>; all(): Promise<T[]>;
get(id: string): Promise<T>; get(id: string): Promise<T>;
add(item: any): Promise<void>; add(item: any): Promise<T>;
update(id: string, item: T): Promise<void>; update(id: string, item: T): Promise<void>;
delete(id: string): Promise<void>; delete(id: string): Promise<void>;
} }

View file

@ -1,7 +1,6 @@
import IService from "."; import IService from ".";
import Project from "../types/Project"; import Project from "../types/Project";
import HttpHandler from "./http"; import HttpHandler from "./http";
import User from "../types/User";
interface NewProject { interface NewProject {
title: string; title: string;
@ -26,8 +25,10 @@ export default class ProjectService implements IService<Project> {
return body ?? ({} as Project); return body ?? ({} as Project);
}; };
add = async (item: NewProject): Promise<void> => { add = async (item: NewProject): Promise<Project> => {
await this.http.post(this.path, item, this.key); 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<void> { update(id: string, item: Project): Promise<void> {

View file

@ -30,8 +30,10 @@ export default class TicketService implements IService<Ticket> {
return body ?? ({} as Ticket); return body ?? ({} as Ticket);
}; };
add = async (item: NewTicket): Promise<void> => { add = async (item: NewTicket): Promise<Ticket> => {
await this.http.post(this.path, item, this.key); 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<void> { update(id: string, item: Ticket): Promise<void> {

View file

@ -19,8 +19,10 @@ export default class UserService implements IService<User> {
return body ?? ({} as User); return body ?? ({} as User);
}; };
add = async (item: User): Promise<void> => { add = async (item: User): Promise<User> => {
await this.http.post(this.path, item, this.key); 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<void> => { update = async (id: string, item: User): Promise<void> => {