aretherecookies-mobile/js/apis/Auth0.js
2020-04-05 13:54:34 -05:00

53 lines
1.2 KiB
JavaScript

// @flow
import Auth0 from 'react-native-auth0';
import { type EmailAndPassword } from '../AuthManager';
import { auth0ClientId } from '../constants/AppConstants';
let auth0Instance = null;
export type Auth0User = {
email: string,
accessToken: string,
expiresIn: number,
idToken: string,
};
const getAuth0Instance = () => {
return (
auth0Instance ||
new Auth0({
domain: 'wheresthetp.auth0.com',
clientId: auth0ClientId,
})
);
};
export const loginAuth0 = async ({ email, password }: EmailAndPassword): Promise<?Auth0User> => {
const creds = await getAuth0Instance().auth.passwordRealm({
username: email,
password,
realm: 'Username-Password-Authentication',
});
return {
email,
...creds,
};
};
export const createAuth0User = async ({ email, password }) => {
const creds = await getAuth0Instance().auth.createUser({
email,
username: email,
password,
connection: 'Username-Password-Authentication',
});
return creds;
};
export const resetAuth0Password = async () => {
const creds = await getAuth0Instance().webAuth.authorize({
scope: 'openid profile email',
audience: 'https://wheresthetp.auth0.com/userinfo',
});
return creds;
};