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()
{
Id = userDto.Id,
FirstName = userDto.FirstName,
LastName = userDto.LastName,
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 { TicketService } from "../../services";
import { useAuth0 } from "../../authentication/auth0";
import { getUID } from "../../authentication/helpers";
interface IProps {
show: boolean;
@ -46,7 +47,7 @@ const NewTicketModal: FC<IProps> = ({ 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,

View file

@ -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 (
<Grid container component="main" className={classes.root}>

View file

@ -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<User>({} as User);
const [account, setAccount] = useState<User>({} 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<void> => {
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 <ErrorController error={error} />;
}
const viewModel = new UserVM(user);
const viewModel = new UserVM(account);
return isLoading ? <Preloader /> : <UserPage viewModel={viewModel} />;
};

View file

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

View file

@ -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<Project> {
return body ?? ({} as Project);
};
add = async (item: NewProject): Promise<void> => {
await this.http.post(this.path, item, this.key);
add = async (item: NewProject): Promise<Project> => {
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> {

View file

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

View file

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