Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Login system #36

Merged
merged 4 commits into from
Jul 9, 2024
Merged

Login system #36

merged 4 commits into from
Jul 9, 2024

Conversation

ezzhang8
Copy link
Collaborator

@ezzhang8 ezzhang8 commented Jul 9, 2024

General behaviour: Users are issued a token upon signup that is valid for 1 day. When this token expires, the user is signed out the next time they use the website. When they login again, a new token is reissued. The token uniquely identifies users of the website when the server handles requests.

You will need to create the column expiry_date on the users table.

ALTER TABLE users
ADD COLUMN expiry_date timestamp;

Backend:

def auth(auth_header) # will return the user ID of the user that called your API endpoint.

Usage:
This is an example endpoint for using login features

@app.route("/api/authorize", methods=['POST'])
@cross_origin()
def authorize() -> Response: 
    try: 
        user_id = auth(request.headers.get("Authorization")) 
        # pass in the whole header to get the user ID

        return jsonify({"success": True, "user_id": user_id}), 200 
    except Exception as e:
        return jsonify({"success": False, "error": str(e)}), 403 
        # Catch the exception if user isn't authorized

Frontend:

Import useAuth on any page as such, works on any component

import { useAuth } from '@/components/auth/AuthContext';

Usage:

const { auth } = useAuth();

auth.token gives the user's token.
auth.email and auth.username are also available.

See /components/Navbar for example on hiding information to logged-in users only.

@ezzhang8 ezzhang8 merged commit 6147981 into main Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants