Giter VIP home page Giter VIP logo

Comments (9)

Globik avatar Globik commented on May 27, 2024

This is how to use koa and koa-passport
https://github.com/Globik/alikon/blob/master/config/auth2.js

from koa-passport.

Globik avatar Globik commented on May 27, 2024

pub.post('/login', (ctx,next)=>{
if(ctx.isAuthenticated()){
if(ctx.state.xhr){
ctx.throw(409, 'Schon authenticated!')
}else{
return ctx.redirect('/')
}
}
return passport.authenticate('local', (err,user,info,status)=>{
if(ctx.state.xhr){
if(err){ctx.body={success:false,info:err.message}; ctx.throw(500,err.message);}
if(user===false){
ctx.body={success:false,info:info.message}
ctx.throw(401,info.message)
}else{
ctx.body={success:true,info:info.message, redirect:ctx.session.dorthin || '/'}
return ctx.login(user)
}
}else{
if(err){
ctx.session.bmessage={success:false,error:err.message}; return ctx.redirect('/login');
}
if(user===false){
ctx.session.bmessage={success:false, error:info.message};
ctx.redirect('/login')
}else{
ctx.redirect(ctx.session.dorthin || '/')
return ctx.login(user)
}
}
}
)(ctx,next)
})

from koa-passport.

devt3000 avatar devt3000 commented on May 27, 2024

Thanks. That isn't sessionless authentication though, I'm trying to use JWTs here.

from koa-passport.

Globik avatar Globik commented on May 27, 2024

Aha, may be you should to disable session in options in a function passport.authenticate('basic', {session: false}, ....)

from koa-passport.

Globik avatar Globik commented on May 27, 2024

See this https://stackoverflow.com/questions/41537116/passport-js-authorize-with-no-session

from koa-passport.

Globik avatar Globik commented on May 27, 2024

Or may be in a jwt based application you should use this https://github.com/koajs/jwt

from koa-passport.

devt3000 avatar devt3000 commented on May 27, 2024

@Globik I'm using the npm package "jsonwebtoken", it's good.

from koa-passport.

devt3000 avatar devt3000 commented on May 27, 2024

@Globik okay, I will try that asap.

from koa-passport.

devt3000 avatar devt3000 commented on May 27, 2024

if (!user) { ctx.body = { success: false }; ctx.throw(401); } else { const payload = { username: user.username, expires: Date.now() + 1000000 };

  token = jwt.sign(JSON.stringify(payload), secret);
  ctx.cookies.set("jwt", token, { httpOnly: true, secure: false });
  ctx.body = { success: true };

  return ctx.login(user);
}

})(ctx); await next();

This code fixed my issue. Thanks!

from koa-passport.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.