How to redirect after a login success or fails in loopback

357 views
Skip to first unread message

Mrinal Purohit

unread,
Dec 6, 2014, 11:48:28 PM12/6/14
to loopb...@googlegroups.com
I have created a loopback and successfully able to connect to mongo as a datasource.
I created a user and then tried to login. It worked perfectly. As soon as I login from my login screen, I get this:
{
  • id"gxLjLWWozMCk8HLuOuGrhSCqIkIUzAbruwkHAwKaB8TlOIA565tyUnuwlh99kHTF",
  • ttl1209600,
  • created"2014-12-07T04:46:33.900Z",
  • userId"5483c9d6e97d2c6b04ae8798"
}

This means that I was able to login properly. I am using standard route api/Users/login
I don't want this screen to show up. I need to redirect the user to some dashboard. In case it fails, it should redirect it back to the login screen.

Brian Bagdasarian

unread,
Dec 7, 2014, 12:11:22 AM12/7/14
to Mrinal Purohit, loopb...@googlegroups.com

Add a hook for the redirect that fires after the log in action

--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mrinal Purohit

unread,
Dec 7, 2014, 12:16:15 AM12/7/14
to loopb...@googlegroups.com, mri...@mrinallabs.com
A quick question, I have created a separate model customers that has a base of 'User'. Now I want that email should not be a mandatory field while registering a user. Only username should be. To do this, I defined customer.json as
{
"name": "customers",
"base": "User",
"idInjection": true,
"properties": {
"NAME": {
"type": "string",
"required": false
},
"email":{
"type":"string",
"required":false
},
"username":{
"type":"string",
"required":false
}

},
"validations": [],
"relations": {},
"acls": [],
"methods": []
}

but not working!

Brian Bagdasarian

unread,
Dec 7, 2014, 12:21:58 AM12/7/14
to Mrinal Purohit, loopb...@googlegroups.com

First, it looks like your json isn't quite correct Model Name. Should be customer.   Plural is customers.   Second,  make sure that the fields you are overriding are named properly (and the same) . Third,   what is in your js file for the model?

Brian Bagdasarian

unread,
Dec 7, 2014, 12:22:37 AM12/7/14
to Mrinal Purohit, loopb...@googlegroups.com

One more thing...  How did you create the model?

Mrinal Purohit

unread,
Dec 7, 2014, 12:27:37 AM12/7/14
to loopb...@googlegroups.com, mri...@mrinallabs.com
I created two files inside common->models
1. customers.json (plurals fixed)
      { "name": "customer",

"base": "User",
"idInjection": true,
"properties": {
"email":{
"type":"string",
"required":false
},
"username":{
"type":"string",
"required":false
}

},
"validations": [],
"relations": {},
"acls": [],
"methods": []
}

2. customers.js

module.exports = function(customer){

}

And then i made the following entry in model-config.js
"customer": {
"dataSource": "mango-database",
"public": true
}

Simon Ho

unread,
Dec 10, 2014, 4:03:53 AM12/10/14
to loopb...@googlegroups.com, mri...@mrinallabs.com
See my new example repo at https://github.com/strongloop/loopback-faq-user-management

If you have any more questions beyond those, feel free to ask me again. ;)

86tfv987

unread,
Nov 22, 2015, 8:38:12 PM11/22/15
to LoopbackJS, mri...@mrinallabs.com
Please check out a question about your exapmle https://github.com/strongloop/loopback-example-user-management/issues/26.

I also paste it here:

So you have this line in your example:

if (!req.accessToken) return res.sendStatus(401); 
link: https://github.com/strongloop/loopback-example-user-management/blob/master/server/boot/routes.js#L45


Ok, looks fine. But how do you push the accessToken into req so that the accessToken can be checked out that way?

In your login route I only see this. You only return the accessToken on the client:

      res.render('home', {
        email: req.body.email,
        accessToken: token.id
      });

link https://github.com/strongloop/loopback-example-user-management/blob/master/server/boot/routes.js#L36


And what does happen then? How the accessToken is a property of reqreq.accessToken?

Reply all
Reply to author
Forward
0 new messages