Skip to content

abpvn/cas-client-node

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CAS client node

A cas client for nodejs

Installation

npm i cas-client-node --save

Build

npm run build

Example

import express from 'express';
import CAS from 'cas-client-node';
import session from 'express-session';
const port = 80;
const server = express();
const cas = CAS({
    cas_host: 'https://cas-server.com/cas',
    service: 'http://localhost/sso'
});
server.use(session({
    secret: 'secret',
    resave: false,
    saveUninitialized: true
}))
server.get('/sso', cas.auth, async(req, res, next)=> {
    if (req.user) {
        let expires = new Date();
        expires.setDate(expires.getDate() + 90);
        res.cookie('user', req.user, {expires: expires});
        if (req.headers.referer)
            res.redirect(req.headers.referer);
        else res.redirect('/');
    }
    else {
        res.status(200).send('An error occur when login');
    }
});
server.get('/users/logout', cas.logout, async(req, res, next)=> {
    res.cookie('user', null, {expires: new Date(Date.now() - 1000)});
    res.redirect('/?ref=logout');
});
server.listen(port, () => {   
    console.log(`The server is running at http://localhost:${port}/`);
});

Made with ♥ by hoangrio