I want to implement refresh_token endpoint. However, I am clueless about it, how to use refresh token token to get access token. I have generated refresh token here lie this in login/access-token endpoint. Do I first need to store this refresh token or client has to send it?
I am also unsure whether this is the correct way to generate refresh token below.
@app.post("/login/access-token", response_model=schema.Token)
def login(dbs: Session = Depends(get_db), form_data: OAuth2PasswordRequestForm = Depends()) -> Any:
"""
OAuth2 compatible token login, get an access token for future requests
"""
print('In login')
user = crud.authenticate(
dbs, email=form_data.username, password=form_data.password
)
# print('In login user', user)
if not user:
raise HTTPException(status_code=400, detail="Incorrect email or password")
elif not crud.is_active(user):
raise HTTPException(status_code=400, detail="Inactive user")
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
userdata = {
'user_id': user.id,
'email': user.email,
'provider_type': user.provider_type,
'is_active': user.is_active,
'is_super_user': user.is_super_user
}
print('In login after token')
access_token = security.create_access_token(
user.id, expires_delta=access_token_expires
)
refreshdata = {'token_type': 'refresh', 'user_id': user.id}
refresh_token = security.create_access_token(
refreshdata, expires_delta=timedelta(days=settings.REFRESH_TOKEN_EXPIRE_DAYS)
)
# print('refresh_token', refresh_token)
return {
# "access_token": security.create_access_token(
# user.id, expires_delta=access_token_expires
# ),
"access_token": access_token,
"refresh_token": refresh_token,
"token_type": "bearer",
"user": userdata
}