Skip to content

Instantly share code, notes, and snippets.

@abbaspour
Created April 3, 2020 21:50
Show Gist options
  • Save abbaspour/d0b85471375c1e5099e427ba089e5711 to your computer and use it in GitHub Desktop.
Save abbaspour/d0b85471375c1e5099e427ba089e5711 to your computer and use it in GitHub Desktop.
Auth0 Credentials Login with Session
<!doctype html>
<html lang="en">
<head>
<title>Cross Origin + Silent Authentication</title>
<meta charset="utf-8">
<script src="https://cdn.auth0.com/js/auth0/9.13.1/auth0.min.js"></script>
</head>
<body>
<label for="username">Username</label><input id="username" type="text"/>
<br/>
<label for="password">Password</label><input id="password" type="password"/>
<br/>
<input type="submit" value="Login" onclick="submit()"/>
<div id="result"></div>
<script>
const auth0_clientID = 'CCCCC';
const auth0_tenant = 'TTTT.RR';
const auth0_domain = `${auth0_tenant}.auth0.com`;
const redirect_uri = 'https://app1.com';
const default_connection = 'Username-Password-Authentication';
const auth0js = new auth0.WebAuth({
domain: auth0_domain,
clientID: auth0_clientID,
responseType: 'id_token',
redirectUri: redirect_uri
});
function coauth_login_silent(realm, username, password) {
let url = `https://${auth0_domain}/co/authenticate`;
let data = {
client_id: auth0_clientID,
username: username,
password: password,
realm: realm,
credential_type: "http://auth0.com/oauth/grant-type/password-realm"
};
const params = {
headers: {
'content-type': 'application/json'
},
method: 'POST',
credentials: "include",
body: JSON.stringify(data)
};
fetch(url, params)
.then(data => data.json())
.then(value => {
let login_ticket = value['login_ticket'];
console.log('login_ticket: ' + login_ticket);
auth0js.checkSession({login_ticket: login_ticket}, (err, result) => {
if(err) showResult(err);
else showResult(JSON.stringify(result));
});
})
.catch(err => showResult('error in /co/authenticate call: ' + err));
}
function showResult(msg) {
document.getElementById('result').innerText = msg;
}
function submit() {
let username = document.getElementById('username').value;
let password = document.getElementById('password').value;
coauth_login_silent(default_connection, username, password);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment