forked from aws-amplify/amplify-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): add a default deviceName when remembering device
- Loading branch information
Ashwin Kumar
committed
May 1, 2024
1 parent
d218d81
commit 1485390
Showing
8 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/core/src/utils/deviceName/getDeviceName.native.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { getDeviceName as getDeviceNameNative } from '@aws-amplify/react-native'; | ||
|
||
/** | ||
* Retrieves the device name using name in ios and model in android, | ||
* | ||
* @returns {Promise<string>} A promise that resolves with a string representing the device name. | ||
* | ||
* Example Output: | ||
* ios: 'iPhone' / 'user's iPhone' | ||
* android: 'sdk_gphone64_arm64' | ||
*/ | ||
export const getDeviceName = async (): Promise<string> => getDeviceNameNative(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { NavigatorUA } from './types'; | ||
/** | ||
* Retrieves the device name using the User-Agent Client Hints API if available, | ||
* falling back to the traditional userAgent string if not. | ||
* | ||
* @returns {Promise<string>} A promise that resolves with a string representing the device name. | ||
* | ||
* Example Output: | ||
* navigator.userAgentData: | ||
* 'macOS 14.2.1 arm macOS Not A(Brand/99.0.0.0;Google Chrome/121.0.6167.160;Chromium/121.0.6167.160' | ||
* navigator.userAgent: | ||
* 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/115.0' | ||
*/ | ||
export const getDeviceName = async (): Promise<string> => { | ||
const { userAgentData } = navigator as NavigatorUA; | ||
|
||
if (!userAgentData) return navigator.userAgent; | ||
|
||
const { | ||
platform = '', | ||
platformVersion = '', | ||
model = '', | ||
architecture = '', | ||
fullVersionList = [], | ||
} = await userAgentData.getHighEntropyValues([ | ||
'platform', | ||
'platformVersion', | ||
'architecture', | ||
'model', | ||
'fullVersionList', | ||
]); | ||
|
||
const versionList = fullVersionList | ||
.map((v: { brand: string; version: string }) => `${v.brand}/${v.version}`) | ||
.join(';'); | ||
|
||
const deviceName = [ | ||
platform, | ||
platformVersion, | ||
architecture, | ||
model, | ||
platform, | ||
versionList, | ||
] | ||
.filter(value => value) | ||
.join(' '); | ||
|
||
return deviceName; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
export { getDeviceName } from './getDeviceName'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// WICG Spec: https://wicg.github.io/ua-client-hints | ||
|
||
// https://wicg.github.io/ua-client-hints/#navigatorua | ||
export interface NavigatorUA { | ||
readonly userAgentData?: NavigatorUAData; | ||
} | ||
|
||
// https://wicg.github.io/ua-client-hints/#dictdef-navigatoruabrandversion | ||
interface NavigatorUABrandVersion { | ||
readonly brand: string; | ||
readonly version: string; | ||
} | ||
|
||
// https://wicg.github.io/ua-client-hints/#dictdef-uadatavalues | ||
interface UADataValues { | ||
readonly brands?: NavigatorUABrandVersion[]; | ||
readonly mobile?: boolean; | ||
readonly platform?: string; | ||
readonly architecture?: string; | ||
readonly bitness?: string; | ||
readonly formFactor?: string[]; | ||
readonly model?: string; | ||
readonly platformVersion?: string; | ||
/** @deprecated in favour of fullVersionList */ | ||
readonly uaFullVersion?: string; | ||
readonly fullVersionList?: NavigatorUABrandVersion[]; | ||
readonly wow64?: boolean; | ||
} | ||
|
||
// https://wicg.github.io/ua-client-hints/#dictdef-ualowentropyjson | ||
interface UALowEntropyJSON { | ||
readonly brands: NavigatorUABrandVersion[]; | ||
readonly mobile: boolean; | ||
readonly platform: string; | ||
} | ||
|
||
// https://wicg.github.io/ua-client-hints/#navigatoruadata | ||
interface NavigatorUAData extends UALowEntropyJSON { | ||
getHighEntropyValues(hints: string[]): Promise<UADataValues>; | ||
toJSON(): UALowEntropyJSON; | ||
} |