1
Good night, you guys! I’m in a battle to get back the token I send from my backend to my browser.
Follow my Back code in Python:
class Login(Resource):
def post(self):
parser.add_argument('username', type=str)
parser.add_argument('password', type=str)
args = parser.parse_args()
password = hashlib.md5(args['password'].encode('utf-8')).hexdigest()
sql = "SELECT * FROM usuarios WHERE username = (%s) and password = (%s)"
value = (args['username'], password)
dbcursor.execute(sql, value)
result = dbcursor.fetchall()
if len(result) > 0:
id_user = result[0][0]
username = result [0][1]
print(id_user)
payload = {"id": id_user, "username": username}
print(payload)
authtoken = jwt.encode(payload, config.JWT.secret, algorithm='HS256')
print(authtoken)
data = json.dumps({"id": id_user, "username": result[0][1], "token": authtoken .decode("UTF-8)")})
print(data)
response = Response(str(data),mimetype='application/json')
print(response)
response.headers['x-access-token'] = "JWT" + authtoken.decode("UTF-8")
print(response.headers)
return response
else:
return {"message": "user invalido"}
My component in Angular:
userLogin(){
const username = this.loginForm.get('username').value;
const password = this.loginForm.get('password').value;
this.authService
.authenticate(username, password)
.subscribe((res) => {
if (res ["message"] == 'user valido') {
this.router.navigate(['dashboard'])
}
else {
console.log('Caiu no else')
this.loginForm.reset()
this.platformDetectorService.isPlatformBrower() && this.usernameInput.nativeElement.focus()
}
}
)}
My service at Angular:
authenticate(username: string, password: string) {
return this.http
.post(
API_URL,
{ "username":username, "password":password },
{ observe: 'response' } )
.pipe(tap(res => {
const authToken = res.headers.get('x-access-token')
console.log(authToken)
}))
}
Follow photo of my return in the browser:
Information is coming as shown in the images below:
For the photo to see I get 2 returns
And here’s my first OPTIONS-type comeback:
And here’s my second POST-type comeback:
When I try to access x-access-token by my angular component and printar on the console it returns me 'null' as shown in the first image At the moment I’m only printing to then set it to the site Storage