2020-07-07 10:14:12 +00:00
|
|
|
import { User } from "@saleor/fragments/types/User";
|
2019-09-06 15:30:33 +00:00
|
|
|
|
|
|
|
export const isSupported =
|
|
|
|
navigator.credentials && navigator.credentials.preventSilentAccess;
|
|
|
|
|
2020-07-21 13:55:50 +00:00
|
|
|
export function login<T>(loginFn: (id: string, password: string) => T): T {
|
2019-09-11 14:04:41 +00:00
|
|
|
if (isSupported) {
|
|
|
|
navigator.credentials.get({ password: true }).then(credential => {
|
|
|
|
if (credential instanceof PasswordCredential) {
|
2020-07-21 13:55:50 +00:00
|
|
|
return loginFn(credential.id, credential.password);
|
2019-09-11 14:04:41 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-07-21 13:55:50 +00:00
|
|
|
|
|
|
|
return null;
|
2019-09-06 15:30:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function saveCredentials(user: User, password: string) {
|
2019-09-11 14:04:41 +00:00
|
|
|
if (isSupported) {
|
|
|
|
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);
|
|
|
|
}
|
2019-09-06 15:30:33 +00:00
|
|
|
}
|