52 | );
53 | };
54 |
55 | export default LoginPage;
56 |
--------------------------------------------------------------------------------
/client/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
27 | React App
28 |
29 |
30 |
31 |
32 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/client/README.md:
--------------------------------------------------------------------------------
1 | # Getting Started with Create React App
2 |
3 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
4 |
5 | ## Available Scripts
6 |
7 | In the project directory, you can run:
8 |
9 | ### `yarn start`
10 |
11 | Runs the app in the development mode.\
12 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
13 |
14 | The page will reload if you make edits.\
15 | You will also see any lint errors in the console.
16 |
17 | ### `yarn test`
18 |
19 | Launches the test runner in the interactive watch mode.\
20 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21 |
22 | ### `yarn build`
23 |
24 | Builds the app for production to the `build` folder.\
25 | It correctly bundles React in production mode and optimizes the build for the best performance.
26 |
27 | The build is minified and the filenames include the hashes.\
28 | Your app is ready to be deployed!
29 |
30 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
31 |
32 | ### `yarn eject`
33 |
34 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
35 |
36 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
37 |
38 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
39 |
40 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
41 |
42 | ## Learn More
43 |
44 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45 |
46 | To learn React, check out the [React documentation](https://reactjs.org/).
47 |
--------------------------------------------------------------------------------
/server/app.py:
--------------------------------------------------------------------------------
1 | from flask import Flask, request, jsonify, session
2 | from flask_bcrypt import Bcrypt
3 | from flask_cors import CORS, cross_origin
4 | from flask_session import Session
5 | from config import ApplicationConfig
6 | from models import db, User
7 |
8 | app = Flask(__name__)
9 | app.config.from_object(ApplicationConfig)
10 |
11 | bcrypt = Bcrypt(app)
12 | CORS(app, supports_credentials=True)
13 | server_session = Session(app)
14 | db.init_app(app)
15 |
16 | with app.app_context():
17 | db.create_all()
18 |
19 | @app.route("/@me")
20 | def get_current_user():
21 | user_id = session.get("user_id")
22 |
23 | if not user_id:
24 | return jsonify({"error": "Unauthorized"}), 401
25 |
26 | user = User.query.filter_by(id=user_id).first()
27 | return jsonify({
28 | "id": user.id,
29 | "email": user.email
30 | })
31 |
32 | @app.route("/register", methods=["POST"])
33 | def register_user():
34 | email = request.json["email"]
35 | password = request.json["password"]
36 |
37 | user_exists = User.query.filter_by(email=email).first() is not None
38 |
39 | if user_exists:
40 | return jsonify({"error": "User already exists"}), 409
41 |
42 | hashed_password = bcrypt.generate_password_hash(password)
43 | new_user = User(email=email, password=hashed_password)
44 | db.session.add(new_user)
45 | db.session.commit()
46 |
47 | session["user_id"] = new_user.id
48 |
49 | return jsonify({
50 | "id": new_user.id,
51 | "email": new_user.email
52 | })
53 |
54 | @app.route("/login", methods=["POST"])
55 | def login_user():
56 | email = request.json["email"]
57 | password = request.json["password"]
58 |
59 | user = User.query.filter_by(email=email).first()
60 |
61 | if user is None:
62 | return jsonify({"error": "Unauthorized"}), 401
63 |
64 | if not bcrypt.check_password_hash(user.password, password):
65 | return jsonify({"error": "Unauthorized"}), 401
66 |
67 | session["user_id"] = user.id
68 |
69 | return jsonify({
70 | "id": user.id,
71 | "email": user.email
72 | })
73 |
74 | @app.route("/logout", methods=["POST"])
75 | def logout_user():
76 | session.pop("user_id")
77 | return "200"
78 |
79 | if __name__ == "__main__":
80 | app.run(debug=True)
--------------------------------------------------------------------------------