When the user is authenticated, you will receive a response similar to this from your front end. Your frontend is reponsible for sending this payload to your server.
{"email": "myemail@gmail.com",// User's email (or phone number)"oauth_token": {"access_token":"eyJhbGciOiJFUzI1NiIsImt...",// Access Token to validate"id_token":"eyJhbGciOiJFUzI1Ni...","refresh_token":"27805:CNf76faa8trMhjXM...","expires_in":3600,"token_type":"Bearer","auth_method":"OTP" },"user": {"ID":"abcdefgh-abcd-abcd-abcd-af6f81fb5432",// Cotter User ID"created_at":"2020-07-21T05:50:14.182738Z","updated_at":"2020-07-21T06:00:47.115096Z","deleted_at":"0001-01-01T00:00:00Z","issuer":"<YOUR_API_KEY_ID>","identifier":"putrikarunian@gmail.com" }}
Send this payload to your backend to register or login the user in your database. A typical flow would look like this:
Validate the access token
Check if the email exists in your database
If it doesn't exists: Create a new user
If it exists: Continue login
(Optional) If you want to use your own session tokens, set the cookie here after validating the access token.
(Optional) if you want to use Cotter's tokens, either store Cotter's access token in the cookie or on the front-end side.
from flask import Flaskfrom flask import requestfrom flask_cors import CORSimport requestsfrom jose import jwtCotterJWKSURL="https://www.cotter.app/api/v0/token/jwks"app =Flask(__name__)CORS(app)@app.route('/login', methods=['POST'])deflogin(name=None): req = request.get_json();# Getting jwt key r = requests.get(url = CotterJWKSURL); data = r.json();print(data); public_key = data["keys"][0];# Getting access token and validate it token = req["oauth_token"]["access_token"] resp = jwt.decode(token, public_key, algorithms='ES256', audience=API_KEY_ID)# User Authenticated!# 1) If user doesn't exist, register user to db. Otherwise, continue# 2) Either use Cotter's Access Token for your entire API authorization# OR# You can Generate your JWT Tokens or other session management herereturn resp;
Validating Cotter's Access Token
Read more on how to verify the OAuth Tokens from Cotter here: