-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(browser-sample): add browser sample pages
- Loading branch information
1 parent
15dd191
commit ebc990d
Showing
11 changed files
with
202 additions
and
396 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,5 +20,8 @@ | |
"packages": [ | ||
"packages/*" | ||
] | ||
}, | ||
"alias": { | ||
"superstruct": "superstruct/lib/index.es.js" | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,6 @@ | ||
export const baseUrl = window.location.origin; | ||
export const redirectUrl = `${baseUrl}/callback`; | ||
export const clientId = 'foo'; | ||
|
||
// OIDC domain | ||
export const endpoint = 'https://logto.dev'; |
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 |
---|---|---|
@@ -1,5 +1,17 @@ | ||
import { init } from './app.js'; | ||
import LogtoClient from '@logto/browser'; | ||
|
||
import { endpoint, clientId } from './consts.js'; | ||
import Callback from './pages/Callback.js'; | ||
import Home from './pages/Home.js'; | ||
|
||
import './index.scss'; | ||
|
||
const logtoClient = new LogtoClient({ endpoint, clientId }); | ||
|
||
const app = document.querySelector('#app'); | ||
|
||
init(app); | ||
// Could replace this with a formal router solution | ||
const isCallback = window.location.pathname.startsWith('/callback'); | ||
const render = isCallback ? Callback : Home; | ||
|
||
render(app, logtoClient); |
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,36 @@ | ||
body { | ||
margin: 0; | ||
padding: 0; | ||
font-family: sans-serif; | ||
background: #101419; | ||
color: #dadae0; | ||
text-align: center; | ||
} | ||
|
||
.container { | ||
width: 100%; | ||
padding: 20px; | ||
} | ||
|
||
table { | ||
margin: 50px auto; | ||
table-layout: fixed; | ||
width: 800px; | ||
border: 1px solid #333; | ||
border-spacing: 0; | ||
|
||
th, | ||
td { | ||
padding: 10px; | ||
word-wrap: break-word; | ||
border: 1px solid #333; | ||
} | ||
|
||
th { | ||
font-weight: bold; | ||
} | ||
} | ||
|
||
button { | ||
cursor: pointer; | ||
} |
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,47 @@ | ||
/* eslint-disable @silverhand/fp/no-mutation */ | ||
import { baseUrl } from '../consts.js'; | ||
|
||
const Callback = async (app, logtoClient) => { | ||
if (!logtoClient) { | ||
throw new Error('no logto client found'); | ||
} | ||
|
||
const fragment = document.createDocumentFragment(); | ||
const container = document.createElement('div'); | ||
container.classList.add('container'); | ||
|
||
const h3 = document.createElement('h3'); | ||
h3.innerHTML = 'Authenticating...'; | ||
|
||
container.append(h3); | ||
fragment.append(container); | ||
app.append(fragment); | ||
|
||
await logtoClient.handleSignInCallback(window.location.href); | ||
|
||
if (logtoClient.isAuthenticated) { | ||
h3.innerHTML = 'Signed in!'; | ||
|
||
const h4 = document.createElement('h4'); | ||
container.append(h4); | ||
|
||
// eslint-disable-next-line @silverhand/fp/no-let | ||
let countDown = 3; | ||
h4.innerHTML = `Redirecting back to home page in ${countDown} seconds...`; | ||
|
||
const interval = setInterval(() => { | ||
countDown -= 1; | ||
h4.innerHTML = `Redirecting back to home page in ${countDown} seconds...`; | ||
|
||
if (countDown === 0) { | ||
clearInterval(interval); | ||
window.location.assign(baseUrl); | ||
} | ||
}, 1000); | ||
} else { | ||
h3.innerHTML = 'Sign in failed.'; | ||
} | ||
}; | ||
|
||
export default Callback; | ||
/* eslint-enable @silverhand/fp/no-mutation */ |
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,62 @@ | ||
/* eslint-disable @silverhand/fp/no-mutation */ | ||
import { baseUrl, redirectUrl } from '../consts.js'; | ||
|
||
const Home = (app, logtoClient) => { | ||
if (!logtoClient) { | ||
throw new Error('no logto client found'); | ||
} | ||
|
||
const { isAuthenticated } = logtoClient; | ||
const onClickSignIn = () => logtoClient.signIn(redirectUrl); | ||
const onClickSignOut = () => logtoClient.signOut(baseUrl); | ||
|
||
const fragment = document.createDocumentFragment(); | ||
|
||
const container = document.createElement('div'); | ||
container.classList.add('container'); | ||
|
||
const h3 = document.createElement('h3'); | ||
h3.innerHTML = 'Logto Browser Sample'; | ||
|
||
const button = document.createElement('button'); | ||
button.innerHTML = isAuthenticated ? 'Sign Out' : 'Sign In'; | ||
button.addEventListener('click', isAuthenticated ? onClickSignOut : onClickSignIn); | ||
|
||
container.append(h3, button); | ||
|
||
if (isAuthenticated) { | ||
// Generate display table for ID token claims | ||
const table = document.createElement('table'); | ||
|
||
const thead = document.createElement('thead'); | ||
const tr = document.createElement('tr'); | ||
const thName = document.createElement('th'); | ||
const thValue = document.createElement('th'); | ||
thName.innerHTML = 'Name'; | ||
thValue.innerHTML = 'Value'; | ||
tr.append(thName, thValue); | ||
thead.append(tr); | ||
table.append(thead); | ||
|
||
const tbody = document.createElement('tbody'); | ||
const entries = Object.entries(logtoClient.getIdTokenClaims()); | ||
for (const [key, value] of entries) { | ||
const tr = document.createElement('tr'); | ||
const tdName = document.createElement('td'); | ||
const tdValue = document.createElement('td'); | ||
tdName.innerHTML = key; | ||
tdValue.innerHTML = value; | ||
tr.append(tdName, tdValue); | ||
tbody.append(tr); | ||
} | ||
|
||
table.append(tbody); | ||
container.append(table); | ||
} | ||
|
||
fragment.append(container); | ||
app.append(fragment); | ||
}; | ||
|
||
export default Home; | ||
/* eslint-enable @silverhand/fp/no-mutation */ |
Oops, something went wrong.