3
Good afternoon, you guys!
Earlier I took a question regarding user authentication in my application and after some good tips I was able to authenticate my user, the doubt now is whether the method applied to validate is correct:
Follows code snippet:
This is my backend, done in python:
class Login(Resource):
def post(self):
parser.add_argument('username', type=str)
parser.add_argument('password', type=str)
args = parser.parse_args()
print(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()
print(result)
if len(result) > 0:
return {"message": "user valido"}
else:
return {"message": "user invalido"}
And this is my Angular component:
this.authService
.authenticate(username, password)
.subscribe((res) => {
if(res ["message"] == 'user valido') {
this.router.navigate(['dashboard'])
}
else {
console.log('user invalido')
}
Basically I send a POST to my server and it will check if the user exists in the database, if there will return me a JSON with the message "valid user", if it does not exist returns the message "invalid user". If validated it will direct me to the /Dashboard route.
It is correct that I validate the user according to the JSON q message I receive from my server as I am doing ?
Why you do not return true/false?
– Filipe L. Constante
Filipe, you say that instead of returning: Return {"message": "user invalido"} or Return {"message": "user valido"} return a true or false? In that case I would have to change my component in Angular for if I received true or false and then I would still, no ?
– Fernando Munhoz
Return true; or Return {"message": true}
– Filipe L. Constante