-
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.
Generic login + ExtensionProvider login (#12)
* implement generic login and finalize the login with extension provider * refactoring * refactoring * refactoring around provider type and login methods * support for normal login and login with native token * refactoring, add initDapp function, set nativeAuthCofig at init * revert minify value * fix import * refactoring * refactoring * refactoring * add nativeAuthConfig selector * refactoring empty provider * changelog update * revert minify value
- Loading branch information
1 parent
0cb8598
commit bcc37e0
Showing
30 changed files
with
357 additions
and
232 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './ProviderFactory'; | ||
export * from './providers/ProviderFactory'; | ||
export * from './Logger'; |
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,30 @@ | ||
import { initStore } from 'store/store'; | ||
import { defaultStorageCallback, StorageCallback } from 'store/storage'; | ||
import { setTokenLoginNativeAuthTokenConfig } from 'store/actions/loginInfo/loginInfoActions'; | ||
import { NativeAuthConfigType } from 'services/nativeAuth/nativeAuth.types'; | ||
import { getDefaultNativeAuthConfig } from 'services/nativeAuth/methods/getDefaultNativeAuthConfig'; | ||
|
||
type InitAppType = { | ||
storage?: { | ||
getStorageCallback: StorageCallback; | ||
}; | ||
nativeAuth?: boolean | NativeAuthConfigType; | ||
}; | ||
const defaultInitAppProps = { | ||
storage: { | ||
getStorageCallback: defaultStorageCallback | ||
} | ||
}; | ||
export const initializeDApp = (props?: InitAppType) => { | ||
const { storage, nativeAuth } = { ...defaultInitAppProps, ...props }; | ||
initStore(storage.getStorageCallback); | ||
|
||
if (nativeAuth) { | ||
const nativeAuthConfig: NativeAuthConfigType = | ||
typeof nativeAuth === 'boolean' | ||
? getDefaultNativeAuthConfig() | ||
: nativeAuth; | ||
|
||
setTokenLoginNativeAuthTokenConfig(nativeAuthConfig); | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { nativeAuth } from 'services/nativeAuth'; | ||
import { setAddress } from 'store/actions/account'; | ||
import { | ||
setProviderType, | ||
setTokenLogin | ||
} from 'store/actions/loginInfo/loginInfoActions'; | ||
import { setAccountProvider } from 'core/providers/accountProvider'; | ||
import { | ||
IProvider, | ||
IProviderFactory | ||
} from 'core/providers/types/providerFactory.types'; | ||
import { ProviderFactory } from 'core/providers/ProviderFactory'; | ||
import { nativeAuthConfigSelector } from 'store/selectors'; | ||
import { getState } from 'store/store'; | ||
import { NativeAuthConfigType } from 'services/nativeAuth/nativeAuth.types'; | ||
import { getIsLoggedIn } from 'utils/account/getIsLoggedIn'; | ||
import { getAddress } from 'utils/account/getAddress'; | ||
|
||
async function loginWithoutNativeToken(provider: IProvider) { | ||
await provider.login(); | ||
|
||
const address = provider.getAddress?.(); | ||
|
||
if (!address) { | ||
throw new Error('Address not found'); | ||
} | ||
|
||
setAddress(address); | ||
|
||
return { | ||
address | ||
}; | ||
} | ||
|
||
async function loginWithNativeToken( | ||
provider: IProvider, | ||
nativeAuthConfig: NativeAuthConfigType | ||
) { | ||
const nativeAuthClient = nativeAuth(nativeAuthConfig); | ||
|
||
const loginToken = await nativeAuthClient.initialize({ | ||
noCache: true | ||
}); | ||
|
||
await provider.login({ token: loginToken }); | ||
|
||
const address = provider.getAddress?.(); | ||
const signature = provider.getTokenLoginSignature?.(); | ||
|
||
if (!address) { | ||
throw new Error('Address not found'); | ||
} | ||
|
||
if (!signature) { | ||
throw new Error('Signature not found'); | ||
} | ||
|
||
const nativeAuthToken = nativeAuthClient.getToken({ | ||
address, | ||
token: loginToken, | ||
signature | ||
}); | ||
|
||
setAddress(address); | ||
setTokenLogin({ | ||
loginToken, | ||
signature, | ||
nativeAuthToken, | ||
nativeAuthConfig | ||
}); | ||
|
||
return { | ||
address, | ||
signature, | ||
nativeAuthToken, | ||
loginToken, | ||
nativeAuthConfig | ||
}; | ||
} | ||
|
||
export const login = async ({ | ||
providerConfig | ||
}: { | ||
providerConfig: IProviderFactory; | ||
}) => { | ||
const loggedIn = getIsLoggedIn(); | ||
|
||
if (loggedIn) { | ||
console.warn('Already logged in with:', getAddress()); | ||
return; | ||
} | ||
|
||
const factory = new ProviderFactory(); | ||
const provider = await factory.create(providerConfig); | ||
|
||
if (!provider) { | ||
throw new Error('Provider not found'); | ||
} | ||
|
||
await provider.init?.(); | ||
setAccountProvider(provider); | ||
setProviderType(providerConfig.type); | ||
|
||
const nativeAuthConfig = nativeAuthConfigSelector(getState()); | ||
|
||
if (nativeAuthConfig) { | ||
return await loginWithNativeToken(provider, nativeAuthConfig); | ||
} | ||
|
||
return await loginWithoutNativeToken(provider); | ||
}; |
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
Oops, something went wrong.