0

I can not create a server session in the mobile requests.

I am developing a mobile application. Sign in with LinkedIn to application made only from mobile.

I wrote the rest api with nodejs. I used the passport for linkedin login.Then I tested it from browser.It worked smoothly on browser.

When i make a request to my endpoint(/auth/linkedin) it redirect to linkedin and typing my account information and allow the app .It is redirect again to callback endpoint (auth/linkedin/callback). I am returning information of the logged-in user if success login. I am making process this session information on next requests.

But when I login from mobile, user information is printed webview and I cannot create session.How can i solve this problem. What am I doing wrong?

I am junior. if you see my code wrong please specify for improve my skill.

app.js

const AuthController = require("./router/Auth");

app.use(express.json());
app.use(session({
  secret:'secretkey',
  saveUninitialized: false,
  resave: true,
  cookie: {maxAge: 365*24*60*60*1000}
}));
app.use(passport.initialize());
app.use(passport.session());
app.use('/auth',AuthController);

passport.use(new LinkedInStrategy({
    clientID: config.linkedin.clientID,
    clientSecret: config.linkedin.clientSecret,
    callbackURL: config.baseUrl[env] + "/auth/linkedin/callback",
    scope: config.linkedin.scope
  },
  function(accessToken, refreshToken, profile, done) {
    User.findOne({'linkedin.id' : profile.id}, function(err, user) {
      if (err) return done(err);
      if (user) return done(null, user);
      else {
        // if there is no user found with that linkedin id, create them
        var newUser = new User();

        // set all of the linkedin information in our user model
        newUser.linkedin.id = profile.id;
        newUser.linkedin.token = accessToken;
        newUser.name  = profile.displayName;

        if (typeof profile.emails != 'undefined' && profile.emails.length > 0)
          newUser.email = profile.emails[0].value;

        if(typeof profile.photos != 'undefined' && profile.photos.length> 0)
          newUser.photo = profile.photos[0]

        // save our user to the database
        newUser.save()
        .then(createWallet)
        .then(updateWallet)
        .then(user => {
          return done(null,user)
        })
        .catch(err =>{
          throw err;
        });
      }
    });
  }
));

passport.serializeUser(function(user, done){
    done(null, user.id)
})

passport.deserializeUser(function(id, done) {
    User.getUserById(id, function(err, user) {
      done(err, user);
    });
  });
app.listen(PORT,()=>{
    console.log("Listening...",PORT);
});

function createWallet (user){
  const userId  = user.id;
  return new Promise((resolve,reject) => {
      request(config.blockChain['url']+'/?type=register&userID='+userId,{json:true} ,(err,res,body)=>{
          if(err) reject(err);
          user.wallet = {
            "secret":body.secret,
            "publicKey":body.publicKey
          }
          resolve(user);
      })
  }
  )

}

function updateWallet(user){

return user.save();
}

Auth.js

router.get('/linkedin', passport.authenticate('linkedin'));
router.get('/linkedin/callback', 
passport.authenticate('linkedin',{ failureRedirect: '/'}),(req,res)=>{
    const user = req.user;
    response = {
        status:true,
        msg:"Login is successfull",
        data: user
    }
    res.status(200).json(response);
});
ByBarov
  • 1
  • 3
  • the problem you are experiencing isn't related with the backend code. You are using a web oriented authentication flow (using a webview) to create a session inside a native android application. This could only works with hybrid application or with PWAs. Check this answer, it could help you. Here the Google Login is used as example, but the auth scheme is the same you are using with linkedin. https://stackoverflow.com/questions/35107090/can-i-use-the-passport-google-callback-to-authenticate-android-ios-users – radar155 May 29 '19 at 22:12
  • thanks so much @Radar155 But this solutions is not solved – ByBarov May 30 '19 at 05:53

0 Answers0