Skip to content
This repository was archived by the owner on Jul 2, 2020. It is now read-only.

Commit 212dd03

Browse files
author
Dan
committed
done create user
1 parent a8194b9 commit 212dd03

File tree

5 files changed

+157
-13
lines changed

5 files changed

+157
-13
lines changed

Diff for: api/index.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Multi;
44

55
use Firebase\JWT\JWT;
6+
67
use Firebase\JWT\SignatureInvalidException;
78
use GraphQL\Error\Debug;
89
use GraphQL\Error\FormattedError;
@@ -15,7 +16,7 @@
1516
use function Siler\Encoder\Json\decode;
1617
use function Siler\Functional\Monad\maybe;
1718
use function Siler\GraphQL\{debug, execute, schema};
18-
use function Siler\Swoole\{bearer, http, json, raw};
19+
use function Siler\Swoole\{bearer, http, json, raw,cors};
1920

2021
$base_dir = __DIR__;
2122
require_once "$base_dir/vendor/autoload.php";
@@ -54,7 +55,9 @@
5455
} catch (Throwable $exception) {
5556
Log\error($exception->getMessage());
5657
$result = FormattedError::createFromException($exception);
58+
5759
} finally {
60+
cors('*','authorization,content-type');
5861
json($result);
5962
}
6063
};

Diff for: package-lock.json

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: web/pages/signin.tsx

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { useMutation, useApolloClient } from "@apollo/react-hooks";
2+
import { NextPage } from "next";
3+
import React from "react";
4+
import Button from "../components/button";
5+
import { Container, Section } from "../components/global-style";
6+
import Layout from "../components/layout";
7+
import gql from "graphql-tag";
8+
import cookie from "cookie";
9+
import redirect from "../lib/redirect";
10+
import { withApollo } from "../lib/apollo";
11+
12+
const Signin: NextPage = () => {
13+
const [sigin] = useMutation(gql`
14+
mutation Siginin($email: String = "[email protected]", $password: String = "multi") {
15+
signIn(email: $email, password: $password)
16+
}
17+
`);
18+
19+
const appoloClient = useApolloClient();
20+
21+
return (
22+
<>
23+
<Layout>
24+
<Section>
25+
<Container>
26+
<Button skin="primary" onClick={onSignInClick}>
27+
Entrar
28+
</Button>
29+
</Container>
30+
</Section>
31+
</Layout>
32+
</>
33+
);
34+
35+
async function onSignInClick(event: React.MouseEvent<HTMLButtonElement>) {
36+
event.preventDefault();
37+
38+
try {
39+
const result = await sigin({});
40+
41+
if (result.data && result.data.signIn) {
42+
document.cookie = cookie.serialize("token", result.data.signIn);
43+
console.log(document.cookie);
44+
await appoloClient.cache.reset();
45+
redirect(null, "/signup");
46+
}
47+
48+
console.log(result);
49+
} catch (error) {
50+
// TODO: Handle error
51+
console.error(error);
52+
}
53+
}
54+
};
55+
56+
export default withApollo(Signin);

Diff for: web/pages/signup.tsx

+91-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,51 @@
1+
import { useApolloClient, useMutation } from "@apollo/react-hooks";
12
import { NextPage } from "next";
23
import React, { useRef, useState } from "react";
34
import Button from "../components/button";
45
import { Container, Section } from "../components/global-style";
56
import Layout from "../components/layout";
67
import Input from "../components/input";
78
import { Column } from "../components/column";
9+
import { withApollo } from "../lib/apollo";
10+
import gql from "graphql-tag";
11+
12+
type User = {
13+
id: string;
14+
email: string;
15+
role: Role;
16+
};
17+
18+
enum Role {
19+
ADMINISTRATOR = "ADMINISTRATOR",
20+
COLLABORATOR = "COLLABORATOR"
21+
}
22+
23+
type UserInput = {
24+
email: string;
25+
password: string;
26+
role: Role;
27+
};
828

929
const Signup: NextPage = () => {
1030
var emailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
1131
const email = useRef<HTMLInputElement>(null);
1232
const pass = useRef<HTMLInputElement>(null);
1333
const confirmpass = useRef<HTMLInputElement>(null);
1434
const [validEmail, setValidEmail] = useState(false);
35+
const [passValue, setPassValue] = useState("");
36+
const [emailValue, setEmailValue] = useState("");
1537
const [validPass, setValidPass] = useState(false);
38+
const [emailErrorMessage, setEmailError] = useState("");
39+
const [passErrorMessage, setpassError] = useState("");
40+
const [createUser] = useMutation<{ createUser: User }, { input: UserInput }>(
41+
gql`
42+
mutation CreateUser($input: UserInput!) {
43+
createUser(input: $input) {
44+
id
45+
}
46+
}
47+
`
48+
);
1649

1750
return (
1851
<>
@@ -23,7 +56,7 @@ const Signup: NextPage = () => {
2356
<Column>
2457
<div>
2558
<Input
26-
onChange={onEmailChange}
59+
onChange={validateEmail}
2760
type="text"
2861
placeholder="Email"
2962
ref={email}
@@ -34,7 +67,7 @@ const Signup: NextPage = () => {
3467
</div>
3568
<div>
3669
<Input
37-
onChange={onPassChange}
70+
onChange={validatePass}
3871
type="password"
3972
placeholder="Senha"
4073
ref={pass}
@@ -45,7 +78,7 @@ const Signup: NextPage = () => {
4578
</div>
4679
<div>
4780
<Input
48-
onChange={onPassChange}
81+
onChange={validatePass}
4982
type="password"
5083
placeholder="Confirmar senha"
5184
ref={confirmpass}
@@ -59,40 +92,86 @@ const Signup: NextPage = () => {
5992
</Button>
6093
</Column>
6194
</form>
95+
{renderError()}
6296
</Container>
6397
</Section>
6498
</Layout>
6599
</>
66100
);
67101

68-
function onEmailChange() {
102+
function renderError() {
103+
return (
104+
<div>
105+
<b>{emailErrorMessage} </b>
106+
<br></br>
107+
<b>{passErrorMessage}</b>
108+
</div>
109+
);
110+
}
111+
112+
function validateEmail() {
69113
if (email.current !== null) {
70114
if (emailRegex.test(String(email.current.value).toLocaleLowerCase())) {
115+
setEmailError("");
116+
setEmailValue(email.current.value);
71117
setValidEmail(true);
72118
} else {
119+
setEmailValue("");
120+
setEmailError("Email inválido");
73121
setValidEmail(false);
74122
}
75123
}
76124
}
77125

78-
function onPassChange() {
79-
if (confirmpass.current !== null && pass.current !== null) {
80-
if (
81-
confirmpass.current.value === pass.current.value &&
82-
pass.current.value.length > 5
83-
) {
126+
function validatePass() {
127+
if (
128+
confirmpass.current !== null &&
129+
pass.current !== null &&
130+
validateMinimumPassSize()
131+
) {
132+
if (confirmpass.current.value === pass.current.value) {
133+
setpassError("");
134+
setPassValue(confirmpass.current.value);
84135
setValidPass(true);
85136
} else {
137+
setPassValue("");
138+
setpassError("As senhas não coincidem");
86139
setValidPass(false);
87140
}
88141
}
89142
}
90-
function sendForm(event: React.MouseEvent<HTMLButtonElement>) {
143+
144+
function validateMinimumPassSize() {
145+
if (confirmpass.current !== null) {
146+
if (confirmpass.current.value.length < 5) {
147+
setpassError("A senha deve ter ao menos 6 caracteres");
148+
return false;
149+
} else {
150+
setpassError("");
151+
return true;
152+
}
153+
}
154+
}
155+
async function sendForm(event: React.MouseEvent<HTMLButtonElement>) {
91156
event.preventDefault();
157+
validatePass();
158+
validateEmail();
159+
validateMinimumPassSize();
160+
92161
if (validEmail && validPass) {
93162
console.log("Pass");
163+
const result = await createUser({
164+
variables: {
165+
input: {
166+
email: emailValue,
167+
password: passValue,
168+
role: Role.COLLABORATOR
169+
}
170+
}
171+
});
172+
console.log(result);
94173
}
95174
}
96175
};
97176

98-
export default Signup;
177+
export default withApollo(Signup);

Diff for: web/tests/signin.gql

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mutation Siginin($email: String = "[email protected]", $password: String = "multi") {
2+
signIn(email: $email, password: $password)
3+
}

0 commit comments

Comments
 (0)