Skip to content

Commit 9160722

Browse files
committed
sign up works
1 parent 406798f commit 9160722

File tree

5 files changed

+141
-55
lines changed

5 files changed

+141
-55
lines changed

handler/auth.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -493,18 +493,13 @@ func (router *HttpRouter) IsValidLogin(c echo.Context) error {
493493
}
494494

495495
func (router *HttpRouter) ValidInvite(c echo.Context) error {
496-
var req ValidateInviteRequest
497-
err := c.Bind(&req)
498-
if err != nil {
499-
return c.JSON(http.StatusBadRequest, json_response.NewError("failed to bind request"))
500-
}
501496
invite_id, err := uuid.Parse(c.Param("invite_id"))
497+
token := c.QueryParam("token")
502498
if err != nil {
503499
return c.JSON(http.StatusBadRequest, json_response.NewError("failed to parse invite id"))
504500
}
505-
req.InvitationID = invite_id
506501

507-
if router.app.ValidInvite(req.InvitationID, req.Token) {
502+
if router.app.ValidInvite(invite_id, token) {
508503
return c.JSON(http.StatusOK, json_response.NewMessage("true"))
509504
}
510505

@@ -536,9 +531,4 @@ type (
536531
NewPasswordRequest struct {
537532
NewPassword string `json:"password"`
538533
}
539-
540-
ValidateInviteRequest struct {
541-
InvitationID uuid.UUID `json:"invite_id"`
542-
Token string `json:"token"`
543-
}
544534
)

repository/auth.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,6 @@ func (store PostgresStore) ChangeUserInfo(request models.ChangeUserInfoRequest)
186186
}
187187

188188
func (store PostgresStore) ValidInvite(inviteId uuid.UUID, tokenHash string) bool {
189-
_, err := store.GetInvitation(inviteId, tokenHash)
190-
if err != nil {
191-
return false
192-
}
193-
return true
189+
invite, err := store.GetInvitation(inviteId, tokenHash)
190+
return err == nil && (!invite.ExpiresAt.Before(time.Now()))
194191
}

web/src/signup/Signup.tsx

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,14 @@
11
import { StrictMode } from "react";
22
import { createRoot } from "react-dom/client";
33
import Navbar from "../components/navbar/Navbar"
4-
import TextField from "../components/textfield/Textfield";
54
import './signup.css';
5+
import SignupPanel from "./SignupPanel";
6+
67

7-
interface FormData {
8-
value: string;
9-
isValid: boolean;
10-
error: string;
11-
}
128

13-
const handleTextChange = (data: FormData) => {
14-
console.log('Value:', data.value);
15-
console.log('Is Valid:', data.isValid);
16-
console.log('Error:', data.error);
17-
}
189

1910
createRoot(document.getElementById("root")!).render(
2011
<StrictMode>
2112
<Navbar />
22-
<div className="signup-page">
23-
<div className="signup-card">
24-
<h1 className="signup-title">Sign Up</h1>
25-
<div className="input-container">
26-
<TextField
27-
initialValue="First Name"
28-
label=""
29-
onChange={handleTextChange}/>
30-
<TextField
31-
initialValue="Last Name"
32-
label=""
33-
onChange={handleTextChange}/>
34-
<TextField
35-
initialValue="Create Password"
36-
label=""
37-
type="password"
38-
onChange={handleTextChange}/>
39-
<TextField
40-
initialValue="Re-Type Password"
41-
label=""
42-
type="password"
43-
onChange={handleTextChange}/>
44-
</div>
45-
<a href="/faq" className="why-am-i-here">Why am I here?</a>
46-
<button className="submit-button">Sign Up</button>
47-
</div>
48-
</div>
13+
<SignupPanel />
4914
</StrictMode>)

web/src/signup/SignupPanel.tsx

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { useEffect, useState } from "react";
2+
import TextField, { TextFieldInput } from "../components/textfield/Textfield";
3+
4+
function SignupPanel() {
5+
const [error, setError] = useState("");
6+
const [loading, setLoading] = useState(true);
7+
8+
const [firstName, setFirstName] = useState("");
9+
const [lastName, setLastName] = useState("");
10+
const [password1, setPassword1] = useState("");
11+
const [password2, setPassword2] = useState("");
12+
13+
const alterStateFromTextfield = (alterState: (newValue: string) => void) => {
14+
return (data: TextFieldInput) => alterState(data.value);
15+
}
16+
17+
const urlParams = new URLSearchParams(window.location.search);
18+
19+
const inviteId = urlParams.get('id');
20+
const token = urlParams.get('token');
21+
const verifyInvite = async () => {
22+
var response = await fetch(`/api/auth/invite/${inviteId}/valid?token=${token}`)
23+
if (response.ok) {
24+
var json = await response.json();
25+
if (json['message'] !== 'true') {
26+
setError("Invalid invite");
27+
}
28+
console.log("valid invite");
29+
} else {
30+
setError("Invalid invite");
31+
}
32+
setLoading(false);
33+
}
34+
const registerUser = async () => {
35+
if (firstName === "") {
36+
alert("First Name cannot be empty")
37+
} else if (lastName === "") {
38+
alert("Last Name cannot be empty")
39+
} else if (password1 !== password2) {
40+
alert("Passwords do not match")
41+
} else {
42+
var response = await fetch(`/api/auth/register/${inviteId}?token=${token}`, {
43+
method: "POST",
44+
headers: {
45+
'Content-Type': 'application/json'
46+
},
47+
body: JSON.stringify({
48+
first_name: firstName,
49+
last_name: lastName,
50+
password: password1
51+
})
52+
});
53+
if (!response.ok) {
54+
var json = await response.json();
55+
alert(json.error);
56+
} else {
57+
redirectToDashboard();
58+
}
59+
}
60+
}
61+
const redirectToDashboard = () => {
62+
window.location.href = "/dashboard"
63+
}
64+
useEffect(() => {
65+
if (loading) {
66+
verifyInvite();
67+
}
68+
})
69+
70+
71+
return (
72+
<>
73+
{error
74+
?
75+
<div className="errorParent">
76+
<div className="error">
77+
{error}
78+
</div>
79+
</div>
80+
:
81+
<div className="signup-page">
82+
<div className="signup-card">
83+
<h1 className="signup-title">Sign Up</h1>
84+
<div className="input-container">
85+
<TextField
86+
initialValue="First Name"
87+
label=""
88+
onChange={alterStateFromTextfield(setFirstName)}/>
89+
<TextField
90+
initialValue="Last Name"
91+
label=""
92+
onChange={alterStateFromTextfield(setLastName)}/>
93+
<TextField
94+
initialValue="Create Password"
95+
label=""
96+
type="password"
97+
onChange={alterStateFromTextfield(setPassword1)}/>
98+
<TextField
99+
initialValue="Re-Type Password"
100+
label=""
101+
type="password"
102+
onChange={alterStateFromTextfield(setPassword2)}/>
103+
</div>
104+
<a href="/faq" className="why-am-i-here">Why am I here?</a>
105+
<button className="submit-button" onClick={registerUser}>Sign Up</button>
106+
</div>
107+
</div>
108+
}
109+
</>
110+
)
111+
}
112+
export default SignupPanel;

web/src/signup/signup.css

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,26 @@
8383

8484
.submit-button:focus {
8585
background-color: #1E212B;
86+
}
87+
88+
.errorParent {
89+
display: flex;
90+
align-items: center;
91+
justify-content: center;
92+
width: 100%;
93+
}
94+
.error {
95+
background: white;
96+
display: flex;
97+
align-items: center;
98+
justify-content: center;
99+
font-family: 'Azeret Mono';
100+
font-size: 150%;
101+
margin-top: 2%;
102+
padding: 2%;
103+
border: 2px solid black;
104+
border-radius: 25px;
105+
box-shadow: 25px 25px 13.9px -6px rgba(0, 0, 0, 0.25);
106+
color: red;
107+
min-height: 30vh;
86108
}

0 commit comments

Comments
 (0)