📄User Registration
This page describes code related to user registration on Matar.
User Login
The user login for Matar app provides access_token() in form of JSON web tokens.
Describing Code :
Starting the login flow ,user provides a phone numbers on which one-time verification otp (one-time password) is received via google firebase authentication.
Then a specified user name ,received otp and a token from FCM (call Firebase. messaging. token ) has to be submitted. FCM token is received everytime** the user opens the app **to ensure the device has the right token, then update the timestamp and optionally the token if it has changed.
Once all the details specified above are submitted an access and refresh token is generated via create_access_token() which is used to a make JSON web tokens. JSON Web Tokens(JWTs ) are a good way of securely transmitting information between client(app user) and server. JWTs are signed and uses public/private key pairs which ensure the senders are who they say they are.
It calls a function (UniqueEntry.submit_otp) to verify the OTP provided by the user. If there are errors, it returns an error response.It attempts to find a user with the specified phone_number. If the user does not exist, it creates a new user record with the provided details, including phone_number, name, image_url, source, and roles.
User Authentication
Here is a code snippet of how user authentication is protected with the functions below :
Describing Code :
When it comes to user authentication a JSON Web Token is used for authentication if valid you will be able to retrieve the authenticated user information using the get_jwt_identity() function .The jwt_required() is used to protect all the routes or app paths when required authorization.
The user object is fetched from a database using SQLAlchemy based on the user's identity (user_id) obtained from the JWT token.
If a user with the given identity is not found in the database, it returns a JSON response indicating an error with a 403 (Forbidden) status code.
If a user is found in the database, it calls the original function (func) with the user object as an argument and passes any additional arguments and keyword arguments. This allows the original function to access the authenticated user.
Finally, it sets the __name__ attribute of the inner function to match the func function's name. This is done to preserve the name of the original function, which can be useful for debugging and introspection.
Last updated