Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sign up with password sample #5

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions packages/sign-up-with-password/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/** @type {import('eslint').Linter.Config} */
module.exports = {
extends: '@silverhand/eslint-config',
rules: {
'jsx-a11y/no-autofocus': 'off',
'unicorn/prefer-string-replace-all': 'off',
'no-restricted-syntax': 'off',
'@silverhand/fp/no-mutation': 'off',
},
overrides: [
{
files: ['*.config.js', '*.config.ts', '*.d.ts'],
rules: {
'import/no-unused-modules': 'off',
},
},
{
files: ['*.d.ts'],
rules: {
'import/no-unassigned-import': 'off',
},
},
],
};
Binary file added packages/sign-up-with-password/favicon.ico
Binary file not shown.
36 changes: 36 additions & 0 deletions packages/sign-up-with-password/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Logto experience sample</title>
<link rel="icon" href="./favicon.ico" />
<script type="module" src="src/index.ts"></script>
</head>

<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div class="app">
<form>
<img fetchpriority="high" class="logo" src="https://logto.io/logo.svg" alt="Logto branding logo" />
<div class="input-field">
<label for="username">Username</label>
<input type="text" name="username" required />
</div>
<div class="input-field">
<label for="password">Password</label>
<input type="password" name="password" required />
</div>
<div class="input-field">
<label for="confirm-password">Confirm password</label>
<input type="password" name="confirm-password" required />
</div>
<button class="submit-button" type="submit">
<span>Register</span>
<div class="spinner"></div>
</button>
</form>
<div class="error-message hidden"></div>
</div>
</body>
</html>
32 changes: 32 additions & 0 deletions packages/sign-up-with-password/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@logto/experience-sample-password-sign-up",
"description": "A sample project demonstrates how to use Logto Experience API to build a identifier & password sign-up page.",
"author": "Silverhand Inc. <contact@silverhand.io>",
"license": "MIT",
"version": "0.0.0",
"type": "module",
"scripts": {
"precommit": "lint-staged",
"start": "vite",
"dev": "logto-tunnel --verbose & vite",
"build": "tsc -b && vite build",
"lint": "eslint --ext .ts src",
"preview": "vite preview"
},
"devDependencies": {
"@logto/experience-sample-shared": "workspace:^",
"@logto/schemas": "^1.19.0",
"@silverhand/eslint-config": "^6.0.1",
"@silverhand/ts-config": "^6.0.0",
"eslint": "^8.56.0",
"lint-staged": "^15.0.0",
"prettier": "^3.0.0",
"stylelint": "^15.0.0",
"typescript": "^5.5.3",
"vite": "^5.4.0"
},
"stylelint": {
"extends": "@silverhand/eslint-config/.stylelintrc"
},
"prettier": "@silverhand/eslint-config/.prettierrc"
}
1 change: 1 addition & 0 deletions packages/sign-up-with-password/src/include.d/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import 'vite/client';
64 changes: 64 additions & 0 deletions packages/sign-up-with-password/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Api } from '@logto/experience-sample-shared/api';
import { clearError, handleError, setSubmitLoading } from '@logto/experience-sample-shared/utils';
import { InteractionEvent } from '@logto/schemas';

import '@logto/experience-sample-shared/scss/normalized.scss';

const api = new Api({ baseUrl: window.location.origin });

window.addEventListener('load', () => {
const form = document.querySelector('form');

form?.addEventListener('submit', async (event) => {
event.preventDefault();
setSubmitLoading(true);
clearError();

try {
const formData = new FormData(form);
const username = formData.get('username')?.toString();
const password = formData.get('password')?.toString();
const confirmPassword = formData.get('confirm-password')?.toString();

if (!username || !password) {
throw new Error('Username and password are required.');
}

if (password !== confirmPassword) {
throw new Error('Passwords do not match.');
}

/**
* Step 1: Initialize a register type interaction.
*/
await api.experience.initInteraction({ interactionEvent: InteractionEvent.Register });

/**
* Step 2: Ensure the username is not already taken.
*/
await api.experience.addUserProfile({ type: 'username', value: username });

/**
* Step 3: Continue registering with password.
*/
await api.experience.addUserProfile({ type: 'password', value: password });

/**
* Step 4: Identify the user.
*
* Note: Unlike registering with verification code or social, register with identifier and password
* does not require a verification step in prior, but the identification step is still required.
*/
await api.experience.identifyUser({});

/**
* Step 5: Submit the interaction and redirect back to your app after the interaction is completed.
*/
const { redirectTo } = await api.experience.submitInteraction();
window.location.replace(redirectTo);
} catch (error) {
handleError(error);
setSubmitLoading(false);
}
});
});
11 changes: 11 additions & 0 deletions packages/sign-up-with-password/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "@silverhand/ts-config/tsconfig.base",
"compilerOptions": {
"baseUrl": "./",
"outDir": "dist",
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["src"]
}
51 changes: 42 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.