4

I am trying to make a react-app that uses a CAS(Central Authentication Service) for authentication for certain URLs. I am using express to run the server by using a CAS client library koa-cas2 https://www.npmjs.com/package/koa-cas2

Here's my server.js file by which I am running my server.

import express from 'express';
import path from 'path';
import open from 'open';
import compression from 'compression';
import ConnectCas from 'koa-cas2';
import bodyParser from 'body-parser';
import session from 'express-session';
import cookieParser from 'cookie-parser';
const MemoryStore  =  require('session-memory-store')(session);

const port = 3000;
const app = express();

app.use(compression());
app.use(cookieParser());

app.use(express.static('build'));

app.get('*', function (req, res) {
    res.sendFile(path.join(__dirname, '../build/index.html', ));
});

app.use(session({
    name: 'NSESSIONID',
    secret: 'Hello I am a long long long secret',
    store: new MemoryStore(),// or other session store 
    saveUninitialized: false,
    resave: false
}));

var casClient = new ConnectCas({
    debug: true,
    ignore: [
        /\/ignore/
    ],
    match: [],
    servicePrefix: 'http://localhost:3000',
    serverPath: 'http://<myCasServerIp>:8080/',
    paths: {
        validate: '/cas/validate',
        serviceValidate: '/cas/serviceValidate',
        proxy: '/cas/proxy',
        login: '/cas/login',
        logout: '/cas/logout',
        proxyCallback: '/cas/proxyCallback'
    },
    redirect: false,
    gateway: false,
    renew: false,
    slo: true,
    cache: {
        enable: false,
        ttl: 5 * 60 * 1000,
        filter: []
    },
    fromAjax: {
        header: 'x-client-ajax',
        status: 418
    }
});

app.use(casClient.core());

// NOTICE: If you want to enable single sign logout, you must use casClient middleware before bodyParser. 
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.get('/logout', casClient.logout());

// or do some logic yourself 
app.get('/logout', function (req, res, next) {
    // Do whatever you like here, then call the logout middleware 
    casClient.logout()(req, res, next);
});

app.listen(port, function (err) {
    if (err) {
        console.log(err);
    } else {
        open(`http://localhost:${port}`);
    }
});

I am not sure how to properly use it with my React app. If anyone has some experience with this, please share his/her experience with it and tell the proper way to do it.

Shivam Kumar
  • 121
  • 2
  • 8

0 Answers0