diff --git a/src/PushNotifications.js b/src/PushNotifications.js index 9cee7b0..21a39d2 100644 --- a/src/PushNotifications.js +++ b/src/PushNotifications.js @@ -1,6 +1,5 @@ import { Notifications } from 'expo' import * as Permissions from 'expo-permissions' -import NotificationsProvider from './providers/NotificationsProvider' export default class PushNotifications { getToken = async () => { @@ -18,16 +17,7 @@ export default class PushNotifications { return } - let token = await Notifications.getExpoPushTokenAsync() - return token - } - - updateToken = async () => { - let token = await this.getToken() - if (token) { - let provider = new NotificationsProvider() - provider.setToken(token) - } + return Notifications.getExpoPushTokenAsync() } /** diff --git a/src/providers/NotificationsProvider.js b/src/providers/NotificationsProvider.js deleted file mode 100644 index c2aeae4..0000000 --- a/src/providers/NotificationsProvider.js +++ /dev/null @@ -1,7 +0,0 @@ -import ApiConnection from '../ApiConnection' - -export default class NotificationsProvider { - setToken = (token: String) => { - return ApiConnection.put('notifications/token', { token: token }) - } -} diff --git a/src/providers/RidersProvider.js b/src/providers/RidersProvider.js index 67f7b8f..74954c4 100644 --- a/src/providers/RidersProvider.js +++ b/src/providers/RidersProvider.js @@ -25,6 +25,10 @@ export default class RidersProvider { return ApiConnection.post('signup', data) } + signIn = (data) => { + return ApiConnection.post('signin', data) + } + loadFriends = () => { return ApiConnection.get('api/users/friends') } diff --git a/src/screens/SignInScreen.js b/src/screens/SignInScreen.js index a867784..2f3fb73 100644 --- a/src/screens/SignInScreen.js +++ b/src/screens/SignInScreen.js @@ -8,7 +8,6 @@ import { import { observer, inject } from 'mobx-react' import * as SecureStore from 'expo-secure-store' import Authentication from '../Authentication' -import ApiConnection from '../ApiConnection' import BulletList from '../components/lists/BulletList' import TerrainIcon from '../components/icons/TerrainIcon' import Colors from '../../constants/Colors' @@ -16,6 +15,7 @@ import Layout from '../../constants/Layout' import Button from '../components/form/Button' import { logger } from '../Logger' import VersionTag from '../components/VersionTag' +import { User } from '../stores/UserStore.mobx' export default @inject('UserStore') @@ -41,20 +41,13 @@ class SignInScreen extends React.Component { this.props.ApplicationStore.updateAccessToken(token.access_token) this.setState({ loadingMessage: 'Signing in...' }) - try { - var signInResult = await ApiConnection.post('signin') - } catch (error) { - logger.error('POST to signin failed', { - error: error - }) - throw new Error('Sign in failed') - } - if (signInResult.success) { - this.handleSignIn(signInResult.user, token) + let user = await this.props.UserStore.signIn() + + if (user) { + this.handleSignIn(user, token) } else { this.setState({ loadingMessage: 'Getting user info...' }) - console.log(await auth.getUserInfo(token.access_token)) this.props.navigation.navigate('SignUp', { user: await auth.getUserInfo(token.access_token), token: token @@ -66,22 +59,13 @@ class SignInScreen extends React.Component { * Set current user ID in ApplicationStore and redirect to App * @memberof SignInScreen */ - handleSignIn = (signInResponse, token) => { - if (!Number.isInteger(signInResponse.id)) { - logger.info('Authentication error:', { - signInResponse: signInResponse, - token: token - }) - throw new Error('Invalid user ID:' + signInResponse.id) - } - + handleSignIn = (user: User, token) => { // On successful login save tokens SecureStore.setItemAsync('refreshToken', token.refresh_token) - this.props.UserStore.upsert(signInResponse) - this.props.ApplicationStore.updateUserId(signInResponse.id) + this.props.ApplicationStore.updateUserId(user.id) // Signed in - logger.info(`User ${signInResponse.id} signed in`) + logger.info(`User ${user.id} signed in`) this.props.navigation.navigate('App') } diff --git a/src/stores/UserStore.mobx.js b/src/stores/UserStore.mobx.js index 69741e1..6cdc870 100644 --- a/src/stores/UserStore.mobx.js +++ b/src/stores/UserStore.mobx.js @@ -4,6 +4,7 @@ import { BaseEntity } from './BaseEntity' import { BaseCollectionStore } from './BaseCollectionStore' import ApplicationStore from './ApplicationStore.mobx' import PushNotifications from '../PushNotifications' +import { logger } from '../Logger' export default class UserStore extends BaseCollectionStore { provider: RidersProvider @@ -16,8 +17,6 @@ export default class UserStore extends BaseCollectionStore { autorun(async (reaction) => { if (this.applicationStore.accessToken && this.applicationStore.userId && !this.loaded) { - let notifications = new PushNotifications() - notifications.updateToken() // FIXME: rather pointless since it no longer waits for user here this.updateLoaded(true) } @@ -65,6 +64,27 @@ export default class UserStore extends BaseCollectionStore { this.updateFriendRequests(friends.requests.received.map((userData) => userData.id)) } + async signUp (user: User) { + return user.saveNew() + } + + async signIn () { + try { + var signInResponse = await this.provider.signIn({ + notificationsToken: await (new PushNotifications()).getToken() + }) + } catch (error) { + logger.error('POST to signin failed', { + error: error.data + }) + throw new Error('Sign in failed') + } + + return signInResponse.success + ? this.upsert(signInResponse.user) + : false + } + /** * Search users by name * @@ -204,7 +224,10 @@ export class User extends BaseEntity { if (this.tempPicture && this.tempPicture.isWeb) { data.picture = this.tempPicture.uri } - let userResponse = await this.store.provider.signUp(data) + let userResponse = await this.store.provider.signUp({ + userInfo: data, + notificationsToken: await (new PushNotifications()).getToken() + }) this.populateFromApiResponse(userResponse)