Skip to content

Commit baa3a54

Browse files
author
Hanyuan Li
committed
pushing changes before demo
1 parent 641be80 commit baa3a54

File tree

8 files changed

+56
-8
lines changed

8 files changed

+56
-8
lines changed

backend/auth/user.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from email_validator import validate_email, EmailNotValidError
44

55
from common.database import get_connection
6-
from common.exceptions import AuthError, InvalidError
6+
from common.exceptions import AuthError, InvalidError, RequestError
77

88
hasher = PasswordHasher(
99
time_cost=2,
@@ -46,10 +46,10 @@ def register(email, password):
4646
try:
4747
normalised = validate_email(email).email
4848
except EmailNotValidError as e:
49-
raise AuthError(description="Invalid email") from e
49+
raise RequestError(description="Invalid email") from e
5050

5151
if User._email_exists(cursor, normalised):
52-
raise AuthError(description="Email already registered")
52+
raise RequestError(description="Email already registered")
5353

5454
hashed = hasher.hash(password)
5555
new_id = User._add_user(conn, cursor, normalised, hashed)

backend/common/exceptions.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
from werkzeug.exceptions import Unauthorized, InternalServerError
1+
from werkzeug.exceptions import BadRequest, Unauthorized, Forbidden, InternalServerError
2+
3+
class IncorrectError(Exception):
4+
def __init__(self, description):
5+
super().__init__()
6+
self.description = description
7+
8+
class RequestError(BadRequest):
9+
pass
210

311
class AuthError(Unauthorized):
412
pass
513

14+
class AccessError(Forbidden):
15+
pass
16+
617
class InvalidError(InternalServerError):
718
pass

backend/routes/auth.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from flask import Blueprint, request, jsonify
2-
from flask_jwt_extended import jwt_required, create_access_token, set_access_cookies, unset_jwt_cookies
2+
from flask_jwt_extended import jwt_required, create_access_token, set_access_cookies, unset_jwt_cookies, verify_jwt_in_request
33

44
from auth.user import User
5+
from common.exceptions import AuthError
56

67
# Constants
78

@@ -33,6 +34,15 @@ def register():
3334

3435
return response, 200
3536

37+
@auth.route("/verify", methods=["GET"])
38+
def verify():
39+
try:
40+
verify_jwt_in_request()
41+
except:
42+
raise AuthError("Not logged in")
43+
44+
return jsonify({}), 200
45+
3646
@auth.route("/logout", methods=["DELETE"])
3747
@jwt_required()
3848
def logout():

frontend/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
2+
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
33

44
import 'bootstrap/dist/css/bootstrap.min.css';
55

frontend/src/components/Header.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from "react";
2+
3+
const Header: React.FC<{}> = () => {
4+
return (
5+
<></>
6+
);
7+
};
8+
9+
export default Header;

frontend/src/components/ProtectedRoute.tsx

Whitespace-only changes.

frontend/src/pages/Home.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
import React, { useState } from "react";
1+
import React, { useEffect, useState } from "react";
22
import { Button, Container } from "react-bootstrap";
3+
import { useNavigate } from "react-router-dom";
4+
5+
import { BACKEND_URI } from "src/config";
36

47
const Home: React.FC<{}> = () => {
58
const [times, setTimes] = useState(0);
69

10+
useEffect(() => {
11+
const verifyToken = async () => {
12+
const result = await fetch(`${BACKEND_URI}/verify`, )
13+
};
14+
15+
verifyToken();
16+
}, []);
17+
718
return (
819
<>
920
<Container className="justify-content-center text-center">

frontend/src/pages/Login.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ const Login: React.FC<{}> = () => {
1414
const [email, setEmail] = useState("");
1515
const [password, setPassword] = useState("");
1616

17+
const validEmailFormat = () => {
18+
return /^([A-Za-z0-9_\-.])+@([A-Za-z0-9_\-.])+\.([A-Za-z]{2,})$/.test(email);
19+
};
20+
1721
const login = async () => {
1822
const result = await fetch(`${BACKEND_URI}/auth/login`, {
1923
method: "POST",
@@ -53,7 +57,10 @@ const Login: React.FC<{}> = () => {
5357
type="password"
5458
onChange={event => setPassword(event.target.value)} />
5559
</Form.Group>
56-
<Button variant="primary" onClick={() => login()}>
60+
<Button
61+
variant="primary"
62+
onClick={() => login()}
63+
disabled={!validEmailFormat() || password === ""}>
5764
Submit
5865
</Button>
5966
</Form>

0 commit comments

Comments
 (0)