react-cas-client is a simple CAS Client for ReactJS for Apereo CAS server (CAS 2.0 and 3.0 protocol)
npm install react-cas-clientyarn add react-cas-clientimport CasClient, { constant } from "react-cas-client";
let casEndpoint = "xxxx.casserver.com";
let casOptions = { version: constant.CAS_VERSION_2_0 };
let casClient = new CasClient(casEndpoint, casOptions);Endpoint of CAS Server (eg. 'xxxx.casserver.com')
path- CAS server service path (eg.'/cas-tmp') (default:'/cas')protocol- CAS server protocol, can be'http','https') (default:'https');version- CAS protocol version can beconstant.CAS_VERSION_2_0,constant.CAS_VERSION_3_0(default:constant.CAS_VERSION_3_0)validation_proxy_path- Proxy path for application to make call to CAS server to validate ticket (!! Related to CORS issue !!)
// Basic usage
casClient
.auth()
.then(successRes => {
console.log(successRes);
// Login user in state / locationStorage ()
// eg. loginUser(response.user);
// Update current path to trim any extra params in url
// eg. this.props.history.replace(response.currentPath);
})
.catch(errorRes => {
console.error(errorRes);
// Error handling
// displayErrorByType(errorRes.type)
// Update current path to trim any extra params in url
// eg. this.props.history.replace(response.currentPath);
});
// Login with gateway
let gateway = true;
casClient
.auth(gateway)
.then(successRes => {})
.catch(errorRes => {});Apply gateway param to CAS login url when gateway is given (Documentation)
- Boolean:
true/false(default:false)
constant.CAS_ERROR_FETCH- Error when validating ticket with CAS Server:constant.CAS_ERROR_PARSE_RESPONSE- Cannot parse response from CAS serverconstant.CAS_ERROR_AUTH_ERROR- User is not authorized
// Assume current url is https://localhost.com/
// Basic usage
casClient.logout();
// Apply redirect url to CAS logout url
// You can applied redirectPath.
// In this case, https://localhost.com/logout will be applied to logout url
let redirectPath = "/logout";
casClient.logout(redirectPath);Apply redirect url to CAS logout url when refirectPath is given (Documentation)
- String: any path (default:
/)
Update CAS server to set Access-Control-Allow-Origin for you application
Using reverse proxy in your application, we will use ngnix as example.
- Update nginx conf to pass request from
*/cas_proxyto your cas server -https://xxxx.casserver.com/
# nginx.conf
location /cas_proxy {
proxy_pass http://xxxx.casserver.com/;
}
- Apply CAS options -
validation_proxy_pathto'/cas_proxy'
npm run testyarn run testMIT license