How to return token in user registration in Adonis?

Asked

Viewed 128 times

1

I am creating a user registration api and would like to return beyond the registered user, the jwt token.

These are currently my duties:

initializeCreate( {request} ){
    const data = request.only(["username", "password", "permission", "status"])
    return new Promise(function(resolve, reject) {
        user.create(data, function(err, resp, body) {
            if (err) {
                reject(err);
            } else {
                resolve(JSON.parse(body))
            }
        })
    })
}

createUser({ auth }){

var initializePromise = initializeCreate();
initializePromise.then(function(result) {
    const token = await auth.attempt(result.username, result.password)
    return token     
}, function(err) {
    console.log(err);
})}

I suppose I should wait for the event User.create() finish and then perform the auth.attempt so I created Promise, but what would be the best way to do it? I’m currently getting the following error:

Unexpected token const token = await auth.Attempt(result.username, result.password)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.