-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mahmoud Swehli
committed
Jul 20, 2023
1 parent
92e7c12
commit 7210578
Showing
9 changed files
with
171 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
example/Muljin.B2CMagicLink.Example/wwwroot/authresult.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Muljin Magic Link Tester</title> | ||
<script type="text/javascript" src="https://alcdn.msauth.net/browser/2.35.0/js/msal-browser.min.js"></script> | ||
</head> | ||
<body> | ||
<h1>Auth results:</h1> | ||
<div id="output">...</div> | ||
|
||
<script> | ||
var muljin = muljin || {}; | ||
const msalConfig = { | ||
auth: { | ||
clientId: '64257a7a-3240-4021-8860-45af0bbd6734', | ||
authority: 'https://muljin.b2clogin.com/tfp/14182de3-6b9b-4138-a0fa-e4107db293e5/B2C_1A_SIGNIN_WITH_EMAIL', | ||
knownAuthorities: ['https://muljin.b2clogin.com/tfp/14182de3-6b9b-4138-a0fa-e4107db293e5/B2C_1A_SIGNIN_WITH_EMAIL'] | ||
} | ||
}; | ||
|
||
function loadMsal(){ | ||
muljin.output = document.querySelector('#output'); | ||
muljin.msalInstance = new msal.PublicClientApplication(msalConfig); | ||
muljin.msalInstance.initialize().then(checkForCode); | ||
} | ||
|
||
function checkForCode(){ | ||
muljin.output.innerHTML = "Checking for code..."; | ||
var code = window.location.search.split('code=')[1]; | ||
if(code !=null && code != undefined){ | ||
console.log(code); | ||
muljin.output.innerHTML = "Code found. Exchanging for token..."; | ||
exchangeCodeForToken(code); | ||
} | ||
else{ | ||
muljin.output.innerHTML = "No code found"; | ||
} | ||
} | ||
|
||
function exchangeCodeForToken(code){ | ||
muljin.msalInstance.acquireTokenByCode({code: code, scopes: ['openid'], redirectUri: "https://jwt.ms"}).then(function(response){ | ||
console.log(response); | ||
muljin.output.innerHTML = "Token received: " + response.accessToken; | ||
}) | ||
.catch(function(error){ | ||
console.log(error); | ||
muljin.output.innerHTML = "Error: " + error; | ||
}); | ||
} | ||
|
||
window.addEventListener('load', loadMsal); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Muljin Magic Link Tester</title> | ||
</head> | ||
<body> | ||
<h2>Enter your email above to get a magic link</h2> | ||
<div> | ||
<input type="email" > | ||
<button>Login</button> | ||
<div id="output"></div> | ||
</div> | ||
|
||
<script> | ||
var muljin = muljin || {}; | ||
|
||
(function() | ||
{ | ||
|
||
function setup(){ | ||
const button = document.querySelector('button'); | ||
const input = document.querySelector('input'); | ||
const output = document.querySelector('#output'); | ||
|
||
var m = new MagicLinkExampe(button, input, output); | ||
} | ||
|
||
function MagicLinkExampe(button, input, output) | ||
{ | ||
this.button = button; | ||
this.input = input; | ||
this.output = output; | ||
this.called = false; | ||
this.button.addEventListener('click', this.onClick.bind(this)); | ||
console.log("Ready"); | ||
} | ||
|
||
MagicLinkExampe.prototype.onClick = function(event) | ||
{ | ||
console.log("Called onclick"); | ||
if (event) { | ||
event.preventDefault(); | ||
} | ||
|
||
if(this.called){ | ||
return; | ||
} | ||
this,output.innerHTML = "Sending magic link to " + this.input.value; | ||
|
||
this.called = true; | ||
this.button.disabled = true; | ||
this.input.disabled = true; | ||
|
||
//xhr request | ||
var req = new XMLHttpRequest(); | ||
//on state change events. Assume 200 is only correct status, anything over 400 is an error. everything else ignored for now. | ||
var me = this; | ||
req.onreadystatechange = function () { | ||
if (req.status === 200) { | ||
this.called = false; | ||
this.output.innerHTML = "Request sent. Please check your email."; | ||
} else if (req.status >= 400) { | ||
this.called = false; | ||
this.output.innerHTML = "Sorry there was an error. Please ensure your email was correct"; | ||
} | ||
}.bind(me); | ||
|
||
var curUrl = window.location.href; | ||
curUrl = curUrl.replace("index.html", ""); | ||
if(!curUrl.endsWith("/")){ | ||
curUrl += "/"; | ||
} | ||
curUrl += "auth/signin?email=" + this.input.value; | ||
|
||
req.open("GET", curUrl); | ||
// req.setRequestHeader("Content-type", "application/json"); | ||
req.send(); | ||
} | ||
|
||
window.addEventListener('load', setup); | ||
|
||
})(); | ||
|
||
</script> | ||
</body> | ||
</html> | ||
|