دنبال کننده ها

۱۳۹۶ مهر ۱۰, دوشنبه

node.js - AdonisJs User Authentication for User Login Fails

[ad_1]



I have started building my first NodeJs app. I am usinf AdonisJs framework.



I have created a simple user registration page from where user gets registered successfully. After that when I go to login page to log that user in, it's request.auth.attempt(email, password) method won't authenticate the existing user. Here's what I have coded so far:





authenticator: 'session',

/*
|--------------------------------------------------------------------------
| Session Authenticator
|--------------------------------------------------------------------------
|
| Session authenticator will make use of sessions to maintain the login
| state for a given user.
|
*/
session:
serializer: 'Lucid',
model: 'App/Model/User',
scheme: 'session',
uid: 'email',
password: 'password'
,




const Route = use('Route')

// Register
Route.get('register', 'AuthController.showRegisterPage')
Route.post('register', 'AuthController.register')

// Login
Route.get('login', 'AuthController.showLoginPage')
Route.post('login', 'AuthController.login')

// Logout
Route.get('logout', 'AuthController.logout')




/**
* Handle user registration
*/
* register(request, response)

// validate form input
const rules = unique:users',
password: 'required

const messages =
'name.required': 'Name field is required.',
'username.required': 'Username field is required.',
'username.unique': 'Username field must have a unique value.',
'email.required': 'Email field is required.',
'email.unique': 'Email field must have a unique value.',
'password.required': 'Password field is required.',
'password.confirmed': 'Password/Confirm Password fields does not match.'


const validation = yield Validator.validateAll(request.all(), rules, messages)

// show error messages upon validation fail
if (validation.fails())
yield request
.withAll()
.andWith( errors: validation.messages() )
.flash()

return response.redirect('back')


const user = new User()
user.name = request.input('name')
user.username = request.input('username')
user.email = request.input('email')
user.password = yield Hash.make(request.input('password'))
yield user.save()

yield response.sendView('auth.register', message: type: 'success', text: 'Registration successful. You can login now.' )






/**
* Handle user authentication
*/
* login(request, response)

const email = request.input('email')
const password = request.input('password')

// validate form input
const rules = email',
password: 'required'


const messages =
'email.required': 'Email field is required.',
'password.required': 'Password field is required.'


const validation = yield Validator.validateAll(request.all(), rules, messages)

// show error messages upon validation fail
if (validation.fails())
yield request
.withAll()
.andWith( errors: validation.messages() )
.flash()

return response.redirect('back')


try

yield request.auth.attempt(email, password)
response.route('/')

catch (e)

yield response.sendView('auth.login', message: type: 'error', text: 'Invalid credentials. Please try again.' )








'use strict'

const Lucid = use('Lucid')
const Hash = use('Hash')

class User extends Lucid

static boot ()
super.boot()

/**
* Hashing password before storing to the
* database.
*/
this.addHook('beforeCreate', function * (next)
this.password = yield Hash.make(this.password)
yield next
)


apiTokens ()
return this.hasMany('App/Model/Token')




module.exports = User


Every time I try to log in, it always executes the catch part in login method. I don't have any idea what's going on. Somebody please help me.




[ad_2]

لینک منبع