Introducing Magic URL Login to Appwrite
Tags: opensource, webdev, javascript, news
Published: 2021-09-07 10:17:45

Appwrite 0.10 introduces **Magic URL** as an authentication method, which allows users to create an account without providing a password and login with a URL sent via an E-Mail.

This feature is especially useful if you want to provide a passwordless authentication process for your application.

![Appwrite Dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/koceo7503ykvehk3cibc.png)

# ⚙️ Setup
Let's learn how we can add Magic URL Authentication to a Web Application using our [Web SDK](https://appwrite.io/docs/getting-started-for-web). The same can be done with our [Flutter SDK](https://appwrite.io/docs/getting-started-for-flutter) and [Android SDK](https://appwrite.io/docs/getting-started-for-android).

The first step is to add our Web SDK to our project with NPM like this:

```bash
npm install appwrite --save
```

If you're using a bundler (like [Rollup](https://rollupjs.org/) or [webpack](https://webpack.js.org/)), you can import the Appwrite module when you need it:

```javascript
import { Appwrite } from "appwrite";
```

To install with a CDN (content delivery network) add the following script to your HTML file before you use any Appwrite services:

```html
<script src="https://cdn.jsdelivr.net/npm/appwrite"></script>
```

The next step is to initialize your SDK code with your project ID which can be found in your project settings page:

```js
// Init your Web SDK
const appwrite = new Appwrite();

appwrite
    .setEndpoint('http://localhost/v1') // Your Appwrite Endpoint
    .setProject('455x34dfkj') // Your Appwrite Project ID
;
```

# 🎩 Create a Magic URL

Once your SDK is setup, access the **Account service** and call the [`createMagicURLSession()`](https://appwrite.io/docs/client/account?sdk=web#accountCreateMagicURLSession) method. The method accepts an e-mail address and a redirect URL as arguments.

```js
// Initiate the Magic URL login
appwrite.account.createMagicURLSession('name@example.com', 'http://localhost/')
    .then(response => {
        console.log(response); // Success
    }, error => {
        console.log(error); // Failure
    });
```

If the [`createMagicURLSession()`](https://appwrite.io/docs/client/account?sdk=web#accountCreateMagicURLSession) method completes without error, the request sends the user an email with a URL containing a secret key for the next step. When the user clicks the link, they are redirected back to the URL you provided with the secret key and userId values attached to the URL query string. This link is valid for 1 hour. If the e-mail passed did not belong to any existing user - this request will also create a user for the passed e-mail address.

# 🔐 Login with a Magic URL

Now that the user is able to initialize the authentication process, we need to complete it by handling the redirect from the URL provided in the e-mail.

Use the [`updateMagicURLSession()`](https://appwrite.io/docs/client/account?sdk=web#accountUpdateMagicURLSession) method and call it with the secret and userId values from the URL's query string.

> Please note that in order to avoid a [Redirect Attack](https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.md) the only valid redirect URLs are the ones from domains you have set when adding your platforms in the console interface.


```js
const urlParams = new URLSearchParams(window.location.search);
const userId = urlParams.get('userId');
const secret = urlParams.get('secret');

let promise = appwrite.account.updateMagicURLSession(userId, secret);

promise.then(function (response) {
    console.log(response); // Success
}, function (error) {
    console.log(error); // Failure
});
```

If the [`updateMagicURLSession()`](https://appwrite.io/docs/client/account?sdk=web#accountUpdateMagicURLSession) succeeded, the user is now logged in. Note that once a link is used, it cannot be used again.

# 🏁 Conclusion

If you need help or encounter any difficulties setting up Magic URL Login with Appwrite, please [join our Discord](https://appwrite.io/discord).

## 🔖 References

- [Appwrite Discord](https://appwrite.io/discord)
- [Appwrite Documentation](https://appwrite.io/docs)
- [Appwrite Homepage](https://appwrite.io)