def post(self): args = self.reqparse.parse_args() user = User.authenticate(args) if user: login_user(user) return dict(user=user.name()), 200 return 'Invalid username or password', 400
def test_authenticate_with_valid_params_returns_user(self): user = user_datastore.create_user( email='*****@*****.**', password=encrypt_password('example'), first_name='Colossus', last_name='Zadeh' ) db.session.commit() result = User.authenticate(dict( email='*****@*****.**', password='******' )) self.assertEquals(result, user)
def login(): data = request.get_json() current_user = User.authenticate(**data) if not current_user: return jsonify({ 'message': 'Invalid credentials', 'authenticated': False }), 401 token = jwt.encode( { 'sub': current_user.email, 'iat': datetime.utcnow(), 'exp': datetime.utcnow() + timedelta(minutes=30) }, current_app.config['SECRET_KEY']) return jsonify({'token': token.decode('UTF-8')})