diff --git a/backend/Dockerfile b/backend/Dockerfile index 5d20aa7..300acaa 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -3,8 +3,14 @@ FROM python:3.10.9 WORKDIR /tmp RUN python -m venv venv COPY ./requirements.txt /code/requirements.txt +RUN . venv/bin/activate RUN python -m pip install -r /code/requirements.txt +COPY venv /code/venv WORKDIR /code -COPY ./ /code +COPY app app +COPY main.py main.py +COPY config.py config.py +COPY boot.sh boot.sh +COPY migrations migrations CMD sh boot.sh diff --git a/backend/app/models.py b/backend/app/models.py index 5a7b163..5bb9635 100644 --- a/backend/app/models.py +++ b/backend/app/models.py @@ -1,28 +1,20 @@ from app import db from flask_login import UserMixin from datetime import datetime -from werkzeug.security import check_password_hash, generate_password_hash +from werkzeug.security import generate_password_hash, check_password_hash + class User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True, unique=True) + role = db.Column(db.String(32), index=True) email = db.Column(db.String(120), index=True, unique=True) password_hash = db.Column(db.String(128)) - about_me = db.Column(db.String(140)) last_seen = db.Column(db.DateTime, default=datetime.utcnow) token = db.Column(db.String(32), index=True, unique=True) def __repr__(self): - return f'' - - def to_dict(self): - return { - "id": self.id, - "username": self.username, - "email": self.email, - "about_me": self.about_me, - } - + return f"" def set_password(self, password): self.password_hash = generate_password_hash(password) @@ -30,3 +22,16 @@ class User(UserMixin, db.Model): def check_password(self, password): return check_password_hash(self.password_hash, password) + def to_dict(self): + return { + "id": self.id, + "username": self.username, + "email": self.email, + } + + def from_dict(self, data, new_user=False): + for field in ["role", "username", "email"]: + if field in data: + setattr(self, field, data[field]) + if new_user and "password" in data: + self.set_password(data["password"]) diff --git a/backend/app/routes.py b/backend/app/routes.py index 51b49b7..e45b6ee 100644 --- a/backend/app/routes.py +++ b/backend/app/routes.py @@ -4,7 +4,7 @@ from flask import Response, jsonify, request from app.errors import error_response from flask_login import current_user -from app import login +from app import login, db from app.models import User @@ -18,7 +18,7 @@ def login_route(): data = request.get_json() if current_user.is_authenticated: - return error_response(400, 'A user is already logged in!') + return error_response(400, "A user is already logged in!") if not data.get("user_id") or not data.get("password"): return error_response(400, "Must supply user_id and password") @@ -32,13 +32,38 @@ def login_route(): login_user(user) resp = jsonify(user.to_dict()) - resp.status_code = 200 return resp + @bp.route("/logout", methods=["POST"]) def logout_route(): if not current_user.is_authenticated: return error_response(400, "No users are logged in!") + resp = jsonify(current_user.to_dict()) logout_user() - return Response(status=200) + return resp + + +@bp.route("/register", methods=["POST"]) +def register(): + data = request.get_json() + + required_fields = ["role", "username", "email", "password", "password2"] + for f in required_fields: + if f not in data: + return error_response(400, f"Must supply {f}") + + if User.query.filter_by(username=data["username"]).first(): + return error_response(409, "User with that username already exists") + + if User.query.filter_by(email=data["email"]).first(): + return error_response(409, "User with that email already exists") + + u = User() + u.from_dict(data, new_user=True) + db.session.add(u) + db.session.commit() + + resp = jsonify(u.to_dict()) + return resp diff --git a/backend/boot.sh b/backend/boot.sh index 0743b5d..19b9522 100755 --- a/backend/boot.sh +++ b/backend/boot.sh @@ -2,8 +2,6 @@ . ./venv/bin/activate -pip install -r ./requirements.txt - flask db upgrade exec gunicorn -b :5000 --access-logfile - --error-logfile - main:app diff --git a/backend/migrations/versions/8e48199f1417_.py b/backend/migrations/versions/8e48199f1417_.py new file mode 100644 index 0000000..96f5d0a --- /dev/null +++ b/backend/migrations/versions/8e48199f1417_.py @@ -0,0 +1,36 @@ +"""empty message + +Revision ID: 8e48199f1417 +Revises: 7736bc740f9b +Create Date: 2023-03-18 21:18:21.923362 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '8e48199f1417' +down_revision = '7736bc740f9b' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('user', schema=None) as batch_op: + batch_op.add_column(sa.Column('role', sa.String(length=32), nullable=True)) + batch_op.create_index(batch_op.f('ix_user_role'), ['role'], unique=False) + batch_op.drop_column('about_me') + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('user', schema=None) as batch_op: + batch_op.add_column(sa.Column('about_me', sa.VARCHAR(length=140), nullable=True)) + batch_op.drop_index(batch_op.f('ix_user_role')) + batch_op.drop_column('role') + + # ### end Alembic commands ### diff --git a/frontend/public/index.html b/frontend/public/index.html index aa069f2..104b21e 100644 --- a/frontend/public/index.html +++ b/frontend/public/index.html @@ -1,21 +1,19 @@ - - - - - - - - - - - React App - - - -
- - + + diff --git a/frontend/src/App.css b/frontend/src/App.css index 74b5e05..e69de29 100644 --- a/frontend/src/App.css +++ b/frontend/src/App.css @@ -1,38 +0,0 @@ -.App { - text-align: center; -} - -.App-logo { - height: 40vmin; - pointer-events: none; -} - -@media (prefers-reduced-motion: no-preference) { - .App-logo { - animation: App-logo-spin infinite 20s linear; - } -} - -.App-header { - background-color: #282c34; - min-height: 100vh; - display: flex; - flex-direction: column; - align-items: center; - justify-content: center; - font-size: calc(10px + 2vmin); - color: white; -} - -.App-link { - color: #61dafb; -} - -@keyframes App-logo-spin { - from { - transform: rotate(0deg); - } - to { - transform: rotate(360deg); - } -} diff --git a/frontend/src/App.js b/frontend/src/App.js index aaf3031..e77fd2e 100644 --- a/frontend/src/App.js +++ b/frontend/src/App.js @@ -1,13 +1,13 @@ import "./App.css"; import { Route } from "wouter"; import HomePage from "./pages/HomePage"; +import LoginPage from "./pages/LoginPage"; function App() { return (
- - - + +
); } diff --git a/frontend/src/components/MyNavbar.jsx b/frontend/src/components/MyNavbar.jsx new file mode 100644 index 0000000..658395b --- /dev/null +++ b/frontend/src/components/MyNavbar.jsx @@ -0,0 +1,20 @@ +import { Nav, Container, Navbar, NavDropdown } from "react-bootstrap"; + +const MyNavbar = () => { + return ( + + + LearningTree + + + + + + + ); +}; + +export default MyNavbar; diff --git a/frontend/src/index.js b/frontend/src/index.js index d563c0f..06f12b9 100644 --- a/frontend/src/index.js +++ b/frontend/src/index.js @@ -3,6 +3,7 @@ import ReactDOM from 'react-dom/client'; import './index.css'; import App from './App'; import reportWebVitals from './reportWebVitals'; +import 'bootstrap/dist/css/bootstrap.min.css'; const root = ReactDOM.createRoot(document.getElementById('root')); root.render( diff --git a/frontend/src/pages/HomePage.jsx b/frontend/src/pages/HomePage.jsx index fa54d1c..527d63a 100644 --- a/frontend/src/pages/HomePage.jsx +++ b/frontend/src/pages/HomePage.jsx @@ -1,7 +1,15 @@ +import { Container } from "react-bootstrap"; +import MyNavbar from "../components/MyNavbar"; + const HomePage = () => { return (
-

This is the home page

+ + +
+

This is the home page

+
+
); }; diff --git a/frontend/src/pages/LoginPage.jsx b/frontend/src/pages/LoginPage.jsx new file mode 100644 index 0000000..2699150 --- /dev/null +++ b/frontend/src/pages/LoginPage.jsx @@ -0,0 +1,38 @@ +import React from "react"; +import { Col, Container, Form, Row } from "react-bootstrap"; +import MyNavbar from "../components/MyNavbar"; + +const LoginPage = () => { + return ( + + + +
+ + + Username + + + + + + + + Password + + + + + + +

+ Don't have an account? Register here +

+
+
+
+
+ ); +}; + +export default LoginPage;