witspry-auth-js-client
v1.0.0
Published
Witspry auth javascript library for secure authentication with configurable providers
Downloads
2
Maintainers
Readme
Example Usage
const oauthConfig = {
clientId: 'your-client-id',
clientSecret: 'your-client-secret', // Optional for PKCE and Implicit flows
redirectUri: 'http://example.com/callback',
audience: 'https://api.example.com',
domain: 'https://auth-server.com',
flowType: 'AuthorizationCodePKCE', // or 'Implicit' or 'AuthorizationCode'
scopes: ['read', 'write'],
loginButtonId: 'login-button',
logoutButtonId: 'logout-button',
onAuthComplete: (result) => {
if (result.error) {
console.error('Auth failed:', result.error, result.error_description);
} else if (result.logged_out) {
console.log('Successfully logged out');
} else {
console.log('Auth successful:', result.access_token);
}
}
};
const oauthClient = new OAuthClient(oauthConfig);
// HTML button examples:
// <button id="login-button">Login</button>
// <button id="logout-button">Logout</button>
// Handle callback in your callback route
if (window.location.search.includes('code') || window.location.search.includes('access_token')) {
oauthClient.handleCallback()
.catch(error => console.error('Callback error:', error));
}Html example
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sample HTML</title>
</head>
<body>
<h1>Sample HTML</h1>
<p>This is a sample HTML file.</p>
<div>
<button id="login-button">Login</button>
<button id="logout-button">Logout</button>
</div>
<script src="witspry-auth-js-client-1.0.0-min.js"></script>
<script>
const oauthConfig = {
clientId: 'your-client-id',
redirectUri: 'http://example.com/callback',
audience: 'https://api.example.com',
domain: 'https://auth-server.com',
flowType: 'AuthorizationCodePKCE', // or 'Implicit' or 'AuthorizationCode'
scopes: ['read', 'write'],
loginButtonId: 'login-button',
logoutButtonId: 'logout-button',
onAuthComplete: (result) => {
if (result.error) {
console.error('Auth failed:', result.error, result.error_description);
} else if (result.logged_out) {
console.log('Successfully logged out');
} else {
console.log('Auth successful:', result.access_token);
}
}
};
const oauthClient = new OAuthClient(oauthConfig);
if (window.location.search.includes('code') || window.location.search.includes('access_token')) {
oauthClient.handleCallback()
.catch(error => console.error('Callback error:', error));
}
</script>
</body>
</html>