Add a thin layer of abstraction

This commit is contained in:
dominik-zeglen 2019-09-06 17:30:33 +02:00
parent ae4d3e81ba
commit 6d6312248d
4 changed files with 47 additions and 57 deletions

41
package-lock.json generated
View file

@ -9415,8 +9415,7 @@
}, },
"ansi-regex": { "ansi-regex": {
"version": "2.1.1", "version": "2.1.1",
"bundled": true, "bundled": true
"optional": true
}, },
"aproba": { "aproba": {
"version": "1.2.0", "version": "1.2.0",
@ -9434,13 +9433,11 @@
}, },
"balanced-match": { "balanced-match": {
"version": "1.0.0", "version": "1.0.0",
"bundled": true, "bundled": true
"optional": true
}, },
"brace-expansion": { "brace-expansion": {
"version": "1.1.11", "version": "1.1.11",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"balanced-match": "^1.0.0", "balanced-match": "^1.0.0",
"concat-map": "0.0.1" "concat-map": "0.0.1"
@ -9453,18 +9450,15 @@
}, },
"code-point-at": { "code-point-at": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true, "bundled": true
"optional": true
}, },
"concat-map": { "concat-map": {
"version": "0.0.1", "version": "0.0.1",
"bundled": true, "bundled": true
"optional": true
}, },
"console-control-strings": { "console-control-strings": {
"version": "1.1.0", "version": "1.1.0",
"bundled": true, "bundled": true
"optional": true
}, },
"core-util-is": { "core-util-is": {
"version": "1.0.2", "version": "1.0.2",
@ -9567,8 +9561,7 @@
}, },
"inherits": { "inherits": {
"version": "2.0.3", "version": "2.0.3",
"bundled": true, "bundled": true
"optional": true
}, },
"ini": { "ini": {
"version": "1.3.5", "version": "1.3.5",
@ -9578,7 +9571,6 @@
"is-fullwidth-code-point": { "is-fullwidth-code-point": {
"version": "1.0.0", "version": "1.0.0",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"number-is-nan": "^1.0.0" "number-is-nan": "^1.0.0"
} }
@ -9591,20 +9583,17 @@
"minimatch": { "minimatch": {
"version": "3.0.4", "version": "3.0.4",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"brace-expansion": "^1.1.7" "brace-expansion": "^1.1.7"
} }
}, },
"minimist": { "minimist": {
"version": "0.0.8", "version": "0.0.8",
"bundled": true, "bundled": true
"optional": true
}, },
"minipass": { "minipass": {
"version": "2.3.5", "version": "2.3.5",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"safe-buffer": "^5.1.2", "safe-buffer": "^5.1.2",
"yallist": "^3.0.0" "yallist": "^3.0.0"
@ -9621,7 +9610,6 @@
"mkdirp": { "mkdirp": {
"version": "0.5.1", "version": "0.5.1",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"minimist": "0.0.8" "minimist": "0.0.8"
} }
@ -9694,8 +9682,7 @@
}, },
"number-is-nan": { "number-is-nan": {
"version": "1.0.1", "version": "1.0.1",
"bundled": true, "bundled": true
"optional": true
}, },
"object-assign": { "object-assign": {
"version": "4.1.1", "version": "4.1.1",
@ -9705,7 +9692,6 @@
"once": { "once": {
"version": "1.4.0", "version": "1.4.0",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"wrappy": "1" "wrappy": "1"
} }
@ -9781,8 +9767,7 @@
}, },
"safe-buffer": { "safe-buffer": {
"version": "5.1.2", "version": "5.1.2",
"bundled": true, "bundled": true
"optional": true
}, },
"safer-buffer": { "safer-buffer": {
"version": "2.1.2", "version": "2.1.2",
@ -9812,7 +9797,6 @@
"string-width": { "string-width": {
"version": "1.0.2", "version": "1.0.2",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"code-point-at": "^1.0.0", "code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0", "is-fullwidth-code-point": "^1.0.0",
@ -9830,7 +9814,6 @@
"strip-ansi": { "strip-ansi": {
"version": "3.0.1", "version": "3.0.1",
"bundled": true, "bundled": true,
"optional": true,
"requires": { "requires": {
"ansi-regex": "^2.0.0" "ansi-regex": "^2.0.0"
} }
@ -9869,13 +9852,11 @@
}, },
"wrappy": { "wrappy": {
"version": "1.0.2", "version": "1.0.2",
"bundled": true, "bundled": true
"optional": true
}, },
"yallist": { "yallist": {
"version": "3.0.3", "version": "3.0.3",
"bundled": true, "bundled": true
"optional": true
} }
} }
}, },

View file

@ -1,5 +1,10 @@
import React from "react"; import React from "react";
import {
isSupported as isCredentialsManagementAPISupported,
login as loginWithCredentialsManagementAPI,
saveCredentials
} from "@saleor/utils/credentialsManagement";
import { MutationFunction, MutationResult } from "react-apollo"; import { MutationFunction, MutationResult } from "react-apollo";
import { UserContext } from "./"; import { UserContext } from "./";
import { TypedTokenAuthMutation, TypedVerifyTokenMutation } from "./mutations"; import { TypedTokenAuthMutation, TypedVerifyTokenMutation } from "./mutations";
@ -100,12 +105,8 @@ class AuthProvider extends React.Component<
if (!!token && !user) { if (!!token && !user) {
this.verifyToken(token); this.verifyToken(token);
} else { } else {
if (navigator.credentials && navigator.credentials.preventSilentAccess) { if (isCredentialsManagementAPISupported) {
navigator.credentials.get({ password: true }).then(credential => { loginWithCredentialsManagementAPI(this.login);
if (credential instanceof PasswordCredential) {
this.login(credential.id, credential.password);
}
});
} }
} }
} }
@ -116,24 +117,8 @@ class AuthProvider extends React.Component<
tokenAuthFn({ variables: { email, password } }).then(result => { tokenAuthFn({ variables: { email, password } }).then(result => {
if (result && !result.data.tokenCreate.errors.length) { if (result && !result.data.tokenCreate.errors.length) {
if ( if (isCredentialsManagementAPISupported) {
navigator.credentials && saveCredentials(result.data.tokenCreate.user, password);
navigator.credentials.preventSilentAccess
) {
const {
data: {
tokenCreate: { user }
}
} = result;
const cred = new PasswordCredential({
iconURL: user.avatar ? user.avatar.url : undefined,
id: email,
name: user.firstName
? `${user.firstName} ${user.lastName}`
: undefined,
password
});
navigator.credentials.store(cred);
} }
} }
}); });
@ -146,7 +131,7 @@ class AuthProvider extends React.Component<
logout = () => { logout = () => {
this.setState({ user: undefined }); this.setState({ user: undefined });
if (navigator.credentials && navigator.credentials.preventSilentAccess) { if (isCredentialsManagementAPISupported) {
navigator.credentials.preventSilentAccess(); navigator.credentials.preventSilentAccess();
} }
removeAuthToken(); removeAuthToken();

View file

@ -87,7 +87,7 @@ const LoginCard = withStyles(styles, { name: "LoginCard" })(
<FormSpacer /> <FormSpacer />
<TextField <TextField
fullWidth fullWidth
autoComplete="current-password" autoComplete="password"
label={intl.formatMessage({ label={intl.formatMessage({
defaultMessage: "Password" defaultMessage: "Password"
})} })}
@ -95,7 +95,9 @@ const LoginCard = withStyles(styles, { name: "LoginCard" })(
onChange={handleChange} onChange={handleChange}
type="password" type="password"
value={data.password} value={data.password}
data-tc="password" inputProps={{
"data-tc": "password"
}}
/> />
<FormSpacer /> <FormSpacer />
<div className={classes.buttonContainer}> <div className={classes.buttonContainer}>

View file

@ -0,0 +1,22 @@
import { User } from "@saleor/auth/types/User";
export const isSupported =
navigator.credentials && navigator.credentials.preventSilentAccess;
export function login(loginFn: (id: string, password: string) => void) {
navigator.credentials.get({ password: true }).then(credential => {
if (credential instanceof PasswordCredential) {
loginFn(credential.id, credential.password);
}
});
}
export function saveCredentials(user: User, password: string) {
const cred = new PasswordCredential({
iconURL: user.avatar ? user.avatar.url : undefined,
id: user.email,
name: user.firstName ? `${user.firstName} ${user.lastName}` : undefined,
password
});
navigator.credentials.store(cred);
}