Skip to content

Commit

Permalink
feat: Add magic link api method and sign in. (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorwebdev committed Nov 1, 2020
1 parent 7e9e5fd commit e988987
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
11 changes: 7 additions & 4 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Releases
# Releases

Releases are handled by Semantic release. This document is for forcing and documenting any non-code changes.

### v1.7.0

In this release we added `client.api.sendMagicLinkEmail()` and updated `client.signIn()` to support magic link login by only providing email credentials without a password.

### v1.6.1

In this release we strip out the session data from the URL once it is detected.
Expand All @@ -10,17 +14,16 @@ In this release we strip out the session data from the URL once it is detected.

In this release we added `client.user()`, `client.session()`, and `client.refreshSession()`.


### v1.5.10

In this one we had to roll back the automatic Semantic release, which bumped the version to 2.0.0.

This release containes some breaking changes:

```js
// previously
// previously
let client = new Client()

// this release
let client = new GoTrueClient()
```
```
5 changes: 3 additions & 2 deletions example/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ function App() {
} else {
localStorage.removeItem('email')
}
let { error } = await auth.signIn({ email, password })
let { error, user } = await auth.signIn({ email, password })
if (!error && !user) alert('Check your email for the login link!')
if (error) console.log('Error: ', error.message)
}
async function handleEmailSignUp() {
Expand Down Expand Up @@ -140,7 +141,7 @@ function App() {
type="button"
className="w-full flex justify-center py-2 px-4 border border-transparent text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-500 focus:outline-none focus:border-indigo-700 focus:shadow-outline-indigo active:bg-indigo-700 transition duration-150 ease-in-out"
>
Sign In
{password.length ? 'Sign in' : 'Send magic link'}
</button>
</span>
<span className="block w-full rounded-md shadow-sm">
Expand Down
13 changes: 13 additions & 0 deletions src/GoTrueApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ export default class GoTrueApi {
}
}

/**
* Sends a magic login link to an email address.
* @param email The email address of the user.
*/
async sendMagicLinkEmail(email: string): Promise<{ data: {} | null; error: Error | null }> {
try {
const data = await post(`${this.url}/magiclink`, { email }, { headers: this.headers })
return { data, error: null }
} catch (error) {
return { data: null, error }
}
}

/**
* Sends a reset request to an email address.
* @param email The email address of the user.
Expand Down
6 changes: 5 additions & 1 deletion src/GoTrueClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ export default class GoTrueClient {
}> {
try {
this._removeSession()
let { email, password, provider } = credentials
const { email, password, provider } = credentials
if (email && !password) {
const { error } = await this.api.sendMagicLinkEmail(email)
return { data: null, user: null, error }
}
if (email && password) return this._handeEmailSignIn(email, password)
if (provider) return this._handeProviderSignIn(provider)
else throw new Error(`You must provide either an email or a third-party provider.`)
Expand Down

0 comments on commit e988987

Please sign in to comment.