-
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.
V0.0.20/bitloops authentication (#19)
* added readme * Merge config of initialize and authenticate * Update README * Increased version to 0.0.20 * Added authenticationWithGoogle Co-authored-by: Markos Girgis <markos@bitloops.com>
- Loading branch information
Showing
7 changed files
with
163 additions
and
78 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { v4 as uuid } from 'uuid'; | ||
import { AuthTypes, BitloopsConfig, IBitloopsAuthenticationOptions } from './index'; | ||
import { AuthenticationOptionsType } from '.'; | ||
import Bitloops from './index'; | ||
import { BitloopsUser } from './definitions'; | ||
|
||
class auth { | ||
private static authOptions?: AuthenticationOptionsType; | ||
private static bitloopsConfig: BitloopsConfig; | ||
private static bitloops: Bitloops; | ||
private static authChangeCallback: ((user: BitloopsUser) => void) | null; | ||
|
||
static setBitloops(bitloops: Bitloops) { | ||
auth.bitloops = bitloops; | ||
} | ||
|
||
static setAuthOptions(options?: AuthenticationOptionsType) { | ||
auth.authOptions = options; | ||
} | ||
|
||
static setBitloopsConfig(config: BitloopsConfig) { | ||
auth.bitloopsConfig = config; | ||
} | ||
|
||
static authenticateWithGoogle() { | ||
if (auth.bitloops.authOptions?.authenticationType !== AuthTypes.User) throw new Error('Auth type must be User'); | ||
const sessionUuid = uuid(); | ||
const url = `${auth.bitloops.config.ssl === false ? 'http' : 'https'}://${ | ||
auth.bitloops.config.server | ||
}/bitloops/auth/google?client_id=${auth.bitloops.authOptions.clientId}&provider_id=${auth.bitloops.authOptions.providerId}&session_uuid=${sessionUuid}`; | ||
if (typeof window !== 'undefined') { | ||
window.open(url, '_blank'); | ||
auth.bitloops.subscribe(`workflow-events.auth:${auth.bitloops.authOptions.providerId}:${sessionUuid}`, (user: BitloopsUser) => { | ||
if (auth.authChangeCallback) auth.authChangeCallback(user); | ||
}) | ||
} | ||
} | ||
|
||
static clear() { | ||
const clearedAuthOptions = auth.bitloops.authOptions as IBitloopsAuthenticationOptions; | ||
// TODO communicate logout to REST | ||
clearedAuthOptions.token = null; | ||
if (auth.authChangeCallback) auth.authChangeCallback(null); | ||
auth.bitloops.authOptions = clearedAuthOptions; | ||
} | ||
|
||
registerWithGoogle() {} | ||
getUser() {} | ||
|
||
static onAuthStateChange(authChangeCallback: (user: BitloopsUser) => void) { | ||
if (auth.bitloops.authOptions && auth.bitloops.authOptions.authenticationType === AuthTypes.User) { | ||
auth.authChangeCallback = authChangeCallback; | ||
} else { | ||
|
||
} | ||
} | ||
} | ||
|
||
export default auth; |
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,13 @@ | ||
export type BitloopsUser = { | ||
token: string; | ||
} | null; | ||
|
||
export enum AuthTypes { | ||
Anonymous = 'Anonymous', | ||
Basic = 'Basic', | ||
X_API_KEY = 'X-API-Key', | ||
Token = 'Token', | ||
User = 'User', | ||
FirebaseUser = 'FirebaseUser', | ||
OAuth2 = 'OAuth2', | ||
} |
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.