Created
August 19, 2022 10:12
-
-
Save hnestmann/aed161f1370e4017848590aca2b458a6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const shortCode = 'abcdefgh'; | |
const tenant = 'zzzz_001'; | |
const clientId = '1234567-890a-bcde-f123-4567890abcd'; | |
const clientSecret = 'secret'; | |
const base = `https://${shortCode}.api.commercecloud.salesforce.com/shopper/auth/v1/organizations/${tenant}/oauth2/passwordless`; | |
/** Start Page with a little form, where you can enter your username **/ | |
function start() { | |
const link = dw.web.URLUtils.url('PasswordLess-Send'); | |
response.writer.print(`<html>`); | |
response.writer.print(`<head><title>Passwordless Demo</title></head>`); | |
response.writer.print(`<body><form action="${link}">`); | |
response.writer.print(`<label for="user">User <input name="user"/></label><button>Submit</button>`); | |
response.writer.print(`</form></body>`); | |
response.writer.print(`</html>`); | |
} | |
start.public = true; | |
exports.Start = start; | |
/** Communicates with SLAS **/ | |
function send() { | |
const customer = dw.customer.CustomerMgr.getCustomerByLogin(request.httpParameterMap.user.stringValue); | |
const link = (dw.web.URLUtils.url('PasswordLess-SendEmail').abs()); | |
if (customer) { | |
const url = `${base}/login`; | |
const httpClient = new dw.net.HTTPClient(); | |
httpClient.open('POST', url, clientId, clientSecret); | |
httpClient.setTimeout(3000); | |
httpClient.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); | |
const body = `channel_id=${dw.system.Site.current.ID}&locale=en-us&mode=callback&callback_uri=${link}&user_id=${customer.profile.credentials.login}`; | |
httpClient.send(body); | |
if (httpClient.statusCode == 200) { | |
response.writer.print(`You've got mail`); | |
} | |
} | |
} | |
send.public = true; | |
exports.Send = send; | |
/** Triggered by SLAS callbackui, sends an email to the user which contains the token **/ | |
function sendEmail() { | |
const tokenInfo = JSON.parse(request.httpParameterMap.getRequestBodyAsString()); | |
const link = (dw.web.URLUtils.url('PasswordLess-ClickEmail','token', tokenInfo.token).abs()); | |
const email = tokenInfo.email_id; | |
const customer = dw.customer.CustomerMgr.getCustomerByLogin(tokenInfo.login_id); | |
const mailContent = dw.value.MimeEncodedText(`Dear ${customer.profile.firstName} please click below to login ${link}`) | |
// integration to SMS, Whatsapp, Slack?? | |
const mail = new dw.net.Mail(); | |
mail.addTo(email); | |
mail.setFrom("[email protected]"); | |
mail.setSubject("Your Login at Shop"); | |
mail.setContent(mailContent); | |
var status = mail.send(); | |
response.writer.print(status.code); | |
} | |
sendEmail.public = true; | |
exports.SendEmail = sendEmail; | |
/** Processes the click in the email and calls slas to get JWT **/ | |
function clickEmail() { | |
var url = `${base}/token`; | |
var httpClient = new dw.net.HTTPClient(); | |
httpClient.open('POST', url, clientId, clientSecret); | |
httpClient.setTimeout(3000); | |
httpClient.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); | |
var body = `grant_type=client_credentials&hint=pwdless_login&pwdless_login_token=${request.httpParameterMap.token.stringValue}`; | |
httpClient.send(body); | |
if (httpClient.statusCode == 200) { | |
response.writer.print(httpClient.text); | |
} | |
} | |
clickEmail.public = true; | |
exports.ClickEmail = clickEmail; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment