@@ -35,7 +35,7 @@ You can skip optional steps.
3535Create a SvelteKit project.
3636
3737```sh
38- npm create svelte@next my-app && cd my-app
38+ npm create svelte@next my-app && cd my-app && npm install
3939```
4040{% /section %}
4141{% section #step-3 step=3 title="Install Appwrite" %}
@@ -58,7 +58,7 @@ Find your project's ID in the **Settings** page.
5858Create a new file `src/lib/appwrite.js` and add the following code to it, replace `<YOUR_PROJECT_ID>` with your project ID.
5959
6060```js
61- import { Client, Account} from 'appwrite';
61+ import { Client, Account } from 'appwrite';
6262
6363export const client = new Client();
6464
@@ -71,53 +71,60 @@ export { ID } from 'appwrite';
7171```
7272{% /section %}
7373{% section #step-5 step=5 title="Create a login page" %}
74- Create a new file `src/routes/index .svelte` and add the following code to it .
74+ Replace the contents of `src/routes/+page .svelte` with the following code.
7575
7676```html
7777<script>
7878 import { account, ID } from '$lib/appwrite';
7979
8080 let loggedInUser = null;
8181
82- let email = '';
83- let password = '';
84- let name = '';
85-
8682 async function login(email, password) {
8783 await account.createEmailSession(email, password);
8884 loggedInUser = await account.get();
8985 }
86+
87+ async function register(email, password) {
88+ await account.create(ID.unique(), email, password);
89+ login(email, password);
90+ }
91+
92+ function submit(e) {
93+ e.preventDefault();
94+ const formData = new FormData(e.target);
95+ const type = e.submitter.dataset.type;
96+
97+ if (type === "login") {
98+ login(formData.get('email'), formData.get('password'));
99+ } else if (type === "register") {
100+ register(formData.get('email'), formData.get('password'));
101+ }
102+ }
103+
104+ function logout() {
105+ await account.deleteSession('current');
106+ loggedInUser = null;
107+ }
90108</script>
91109
92110<p>
93111 {loggedInUser ? `Logged in as ${loggedInUser.name}` : 'Not logged in'}
94112</p>
95113
96- <form>
97- <input type="email" placeholder="Email" bind:value={email} />
98- <input type="password" placeholder="Password" bind:value={password} />
99- <input type="text" placeholder="Name" bind:value={name} />
100-
101- <button type="button" on:click={() => login(email, password)}>Login</button>
102- <button
103- type="button"
104- on:click={async () => {
105- await account.create(ID.unique(), email, password, name);
106- login(email, password);
107- }}>Register</button>
108-
109- <button
110- type="button"
111- on:click={async () => {
112- await account.deleteSession('current');
113- loggedInUser = null;
114- }}>Logout</button>
114+ <form on:submit={submit}>
115+ <input type="email" placeholder="Email" name="email" required />
116+ <input type="password" placeholder="Password" name="password" required />
117+
118+ <button type="submit" data-type="login">Login</button>
119+ <button type="submit" data-type="register">Register</button>
115120</form>
121+
122+ <button on:click={logout}>Logout</button>
116123```
117124{% /section %}
118125
119126{% section #step-6 step=6 title="All set" %}
120- Run your project with `npm run dev -- --open --port 3000 ` and open [Localhost on Port 3000 ](http://localhost:3000 ) in your browser.
127+ Run your project with `npm run dev` and open [localhost on port 5173 ](http://localhost:5173 ) in your browser.
121128{% /section %}
122129
123130
0 commit comments