Skip to content

Login hooks #36

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Log the user in with a password.
```js
import { loginWithPassword } from 'meteor-apollo-accounts'

loginWithPassword({username, email, password, plainPassword}, apollo)
loginWithPassword({username, email, password, plainPassword})
```

- ```username```: Optional. The user's username.
Expand All @@ -84,7 +84,7 @@ Change the current user's password. Must be logged in.
```js
import { changePassword } from 'meteor-apollo-accounts'

changePassword({oldPassword, newPassword}, apollo)
changePassword({oldPassword, newPassword})
```

- ```oldPassword```: The user's current password. This is not sent in plain text over the wire.
Expand Down Expand Up @@ -112,7 +112,7 @@ Create a new user.
```js
import { createUser } from 'meteor-apollo-accounts'

createUser({username, email, password, profile}, apollo)
createUser({username, email, password, profile})
```

- ```username```: A unique name for this user.
Expand All @@ -132,7 +132,7 @@ Marks the user's email address as verified. Logs the user in afterwards.
```js
import { verifyEmail } from 'meteor-apollo-accounts'

verifyEmail({token}, apollo)
verifyEmail({token})
```

- ```token```: The token retrieved from the verification URL.
Expand All @@ -147,7 +147,7 @@ Request a forgot password email.
```js
import { forgotPassword } from 'meteor-apollo-accounts'

forgotPassword({email}, apollo)
forgotPassword({email})
```

- ```email```: The email address to send a password reset link.
Expand All @@ -161,7 +161,7 @@ Reset the password for a user using a token received in email. Logs the user in
```js
import { resetPassword } from 'meteor-apollo-accounts'

resetPassword({newPassword, token}, apollo)
resetPassword({newPassword, token})
```

- ```newPassword```: A new password for the user. This is not sent in plain text over the wire.
Expand All @@ -178,7 +178,7 @@ Logins the user with a facebook accessToken
```js
import { loginWithFacebook } from 'meteor-apollo-accounts'

loginWithFacebook({accessToken}, apollo)
loginWithFacebook({accessToken})
```

- ```accessToken```: A Facebook accessToken. It's recommended to use
Expand All @@ -193,7 +193,7 @@ Logins the user with a google accessToken
```js
import { loginWithGoogle } from 'meteor-apollo-accounts'

loginWithGoogle({accessToken}, apollo)
loginWithGoogle({accessToken})
```

- ```accessToken```: A Google accessToken. It's recommended to use
Expand Down
16 changes: 16 additions & 0 deletions client/src/Events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export default {
_cbs: [],
notify (eventName) {
this._cbs.map(cb => {
if (cb.eventName === eventName && typeof cb.callback === 'function') {
cb.callback()
}
})
},
on (eventName, cb) {
this._cbs.push({
eventName: eventName,
callback: cb
})
}
}
7 changes: 4 additions & 3 deletions client/src/changePassword.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import hashPassword from './hashPassword'
import gql from 'graphql-tag'
import hashPassword from './hashPassword'
import {getClient} from './store'

export default async function ({oldPassword, newPassword}, apollo) {
export default async function ({oldPassword, newPassword}) {
if (!oldPassword || !newPassword) throw new Error('Old and new password are required')

const result = await apollo.mutate({
const result = await getClient().mutate({
mutation: gql`mutation changePassword($oldPassword: HashedPassword!, $newPassword: HashedPassword!) {
changePassword(oldPassword: $oldPassword, newPassword: $newPassword) {
success
Expand Down
48 changes: 27 additions & 21 deletions client/src/createUser.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
import hashPassword from './hashPassword'
import gql from 'graphql-tag'
import {storeLoginToken} from './store'
import {handleLoginCallback, getClient, startLoggingIn, endLoggingIn} from './store'

export default async function ({username, email, password, profile}, apollo) {
const result = await apollo.mutate({
mutation: gql`
mutation createUser ($username: String, $email: String, $password: HashedPassword!, $profile: CreateUserProfileInput) {
createUser (username: $username, email: $email, password: $password, profile: $profile) {
id
token
tokenExpires
export default async function ({username, email, password, profile}) {
startLoggingIn()
let result
try {
result = await getClient().mutate({
mutation: gql`
mutation createUser ($username: String, $email: String, $password: HashedPassword!, $profile: CreateUserProfileInput) {
createUser (username: $username, email: $email, password: $password, profile: $profile) {
id
token
tokenExpires
}
}
}
`,
variables: {
username,
email,
password: hashPassword(password),
profile
}
})
`,
variables: {
username,
email,
password: hashPassword(password),
profile
}
})
} catch (err) {
return handleLoginCallback(err)
} finally {
endLoggingIn()
}

const {id, token, tokenExpires} = result.data.createUser
await storeLoginToken(id, token, new Date(tokenExpires))
return id
return handleLoginCallback(null, result.data.createUser)
}
5 changes: 3 additions & 2 deletions client/src/forgotPassword.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import gql from 'graphql-tag'
import {getClient} from './store'

export default async function ({email}, apollo) {
const result = await apollo.mutate({
export default async function ({email}) {
const result = await getClient().mutate({
mutation: gql`mutation forgotPassword($email: String!) {
forgotPassword(email: $email) {
success
Expand Down
48 changes: 44 additions & 4 deletions client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@ import loginWithFacebook from './oauth/loginWithFacebook'
import loginWithGoogle from './oauth/loginWithGoogle'
import loginWithLinkedIn from './oauth/loginWithLinkedIn'
import userId from './userId'
import {onTokenChange, getLoginToken, setTokenStore} from './store'
import {
initWithClient,
setTokenStore,
onLogin,
onLoginFailure,
onLogout,
loggingIn,
TOKEN_EXPIRES_KEY,
TOKEN_KEY,
USER_ID_KEY,
} from './store'

export {
changePassword,
createUser,
forgotPassword,
getLoginToken,
hashPassword,
loginWithPassword,
logout,
Expand All @@ -27,7 +36,38 @@ export {
loginWithFacebook,
loginWithGoogle,
loginWithLinkedIn,
onTokenChange,
userId,
initWithClient,
setTokenStore,
onLogin,
onLoginFailure,
onLogout,
loggingIn,
TOKEN_EXPIRES_KEY,
TOKEN_KEY,
USER_ID_KEY,
}

export default {
changePassword,
createUser,
forgotPassword,
loginWithPassword,
logout,
resendVerificationEmail,
resetPassword,
verifyEmail,
loginWithFacebook,
loginWithGoogle,
loginWithLinkedIn,
userId,
initWithClient,
setTokenStore,
userId
onLogin,
onLoginFailure,
onLogout,
loggingIn,
TOKEN_EXPIRES_KEY,
TOKEN_KEY,
USER_ID_KEY,
}
46 changes: 26 additions & 20 deletions client/src/loginWithPassword.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import hashPassword from './hashPassword'
import gql from 'graphql-tag'
import {storeLoginToken} from './store'
import {handleLoginCallback, getClient, startLoggingIn, endLoggingIn} from './store'

export default async function ({username, email, password}, apollo) {
const result = await apollo.mutate({
mutation: gql`
mutation login ($username: String, $email: String, $password: HashedPassword!) {
loginWithPassword (username: $username, email: $email, password: $password) {
id
token
tokenExpires
export default async function ({username, email, password}) {
startLoggingIn()
let result
try {
result = await getClient().mutate({
mutation: gql`
mutation login ($username: String, $email: String, $password: HashedPassword!) {
loginWithPassword (username: $username, email: $email, password: $password) {
id
token
tokenExpires
}
}
}
`,
variables: {
username,
email,
password: hashPassword(password)
}
})
`,
variables: {
username,
email,
password: hashPassword(password)
}
})
} catch (err) {
return handleLoginCallback(err)
} finally {
endLoggingIn()
}

const {id, token, tokenExpires} = result.data.loginWithPassword
await storeLoginToken(id, token, new Date(tokenExpires))
return id
return handleLoginCallback(null, result.data.loginWithPassword)
}
10 changes: 5 additions & 5 deletions client/src/logout.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {getLoginToken, resetStore} from './store'
import gql from 'graphql-tag'
import {getLoginToken, handleLogout, getClient} from './store'

export default async function (apollo) {
const result = await apollo.mutate({
export default async function () {
const result = await getClient().mutate({
mutation: gql`
mutation logout($token: String!) {
logout(token: $token) {
Expand All @@ -11,10 +11,10 @@ export default async function (apollo) {
}
`,
variables: {
token: await getLoginToken()
token: getLoginToken()
}
})

await resetStore()
await handleLogout()
return result.data.logout.success
}
42 changes: 24 additions & 18 deletions client/src/oauth/loginWithFacebook.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
import gql from 'graphql-tag'
import {storeLoginToken} from '../store'
import {handleLoginCallback, getClient, startLoggingIn, endLoggingIn} from '../store'

/**
* Pass the accessToken
* It's recommended to use https://github.com/keppelen/react-facebook-login
*/

export default async function ({accessToken}, apollo) {
const result = await apollo.mutate({
mutation: gql`
mutation loginWithFacebook ($accessToken: String!) {
loginWithFacebook (accessToken: $accessToken) {
id
token
tokenExpires
export default async function ({accessToken}) {
startLoggingIn()
let result
try {
result = await getClient().mutate({
mutation: gql`
mutation loginWithFacebook ($accessToken: String!) {
loginWithFacebook (accessToken: $accessToken) {
id
token
tokenExpires
}
}
}
`,
variables: {
accessToken
}
})
`,
variables: {
accessToken
}
})
} catch (err) {
return handleLoginCallback(err)
} finally {
endLoggingIn()
}

const {id, token, tokenExpires} = result.data.loginWithFacebook
await storeLoginToken(id, token, new Date(tokenExpires))
return id
return handleLoginCallback(null, result.data.loginWithFacebook)
}
Loading