Skip to content

Commit

Permalink
register-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
benawad committed Aug 10, 2020
1 parent 98afe5f commit b3bc994
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 26 deletions.
18 changes: 13 additions & 5 deletions server/src/resolvers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { MyContext } from "../types";
import { User } from "../entities/User";
import argon2 from "argon2";
import { EntityManager } from "@mikro-orm/postgresql";

@InputType()
class UsernamePasswordInput {
Expand Down Expand Up @@ -78,12 +79,19 @@ export class UserResolver {
}

const hashedPassword = await argon2.hash(options.password);
const user = em.create(User, {
username: options.username,
password: hashedPassword,
});
let user;
try {
await em.persistAndFlush(user);
const result = await (em as EntityManager)
.createQueryBuilder(User)
.getKnexQuery()
.insert({
username: options.username,
password: hashedPassword,
created_at: new Date(),
updated_at: new Date(),
})
.returning("*");
user = result[0];
} catch (err) {
//|| err.detail.includes("already exists")) {
// duplicate username error
Expand Down
10 changes: 4 additions & 6 deletions web/src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ThemeProvider, CSSReset, ColorModeProvider } from "@chakra-ui/core";
import { ThemeProvider, CSSReset } from "@chakra-ui/core";
import { Provider, createClient } from "urql";

const client = createClient({
Expand All @@ -10,14 +10,12 @@ const client = createClient({

import theme from "../theme";

function MyApp({ Component, pageProps }) {
function MyApp({ Component, pageProps }: any) {
return (
<Provider value={client}>
<ThemeProvider theme={theme}>
<ColorModeProvider>
<CSSReset />
<Component {...pageProps} />
</ColorModeProvider>
<CSSReset />
<Component {...pageProps} />
</ThemeProvider>
</Provider>
);
Expand Down
11 changes: 10 additions & 1 deletion web/src/pages/register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,26 @@ import {
import { Wrapper } from "../components/Wrapper";
import { InputField } from "../components/InputField";
import { useRegisterMutation } from "../generated/graphql";
import { toErrorMap } from "../utils/toErrorMap";
import { useRouter } from "next/router";

interface registerProps {}

const Register: React.FC<registerProps> = ({}) => {
const router = useRouter();
const [, register] = useRegisterMutation();
return (
<Wrapper variant="small">
<Formik
initialValues={{ username: "", password: "" }}
onSubmit={async (values) => {
onSubmit={async (values, { setErrors }) => {
const response = await register(values);
if (response.data?.register.errors) {
setErrors(toErrorMap(response.data.register.errors));
} else if (response.data?.register.user) {
// worked
router.push("/");
}
}}
>
{({ isSubmitting }) => (
Expand Down
10 changes: 10 additions & 0 deletions web/src/utils/toErrorMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { FieldError } from "../generated/graphql";

export const toErrorMap = (errors: FieldError[]) => {
const errorMap: Record<string, string> = {};
errors.forEach(({ field, message }) => {
errorMap[field] = message;
});

return errorMap;
};
18 changes: 4 additions & 14 deletions web/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
Expand All @@ -18,12 +14,6 @@
"isolatedModules": true,
"jsx": "preserve"
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules"
]
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}

0 comments on commit b3bc994

Please sign in to comment.