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);
});