Skip to content

Commit

Permalink
Merge pull request #763 from mateuszprzeczek/master
Browse files Browse the repository at this point in the history
feat(config): Add "lang" field with language code value to "useValue"…
  • Loading branch information
Heatmanofurioso authored Apr 24, 2024
2 parents 02e8dee + ec1e0b7 commit 3726f52
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
provide: 'SocialAuthServiceConfig',
useValue: {
autoLogin: false,
lang: 'en',
providers: [
{
id: GoogleLoginProvider.PROVIDER_ID,
Expand Down Expand Up @@ -284,6 +285,7 @@ export class MyCustomLoginProvider extends BaseLoginProvider {
provide: 'SocialAuthServiceConfig',
useValue: {
autoLogin: true,
lang: 'en',
providers: [
{
id: MyCustomLoginProvider.PROVIDER_ID,
Expand Down
2 changes: 2 additions & 0 deletions projects/lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
provide: 'SocialAuthServiceConfig',
useValue: {
autoLogin: false,
lang: 'en',
providers: [
{
id: GoogleLoginProvider.PROVIDER_ID,
Expand Down Expand Up @@ -316,6 +317,7 @@ export class MyCustomLoginProvider extends BaseLoginProvider {
provide: 'SocialAuthServiceConfig',
useValue: {
autoLogin: true,
lang: 'en',
providers: [
{
id: MyCustomLoginProvider.PROVIDER_ID,
Expand Down
2 changes: 1 addition & 1 deletion projects/lib/src/entities/base-login-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SocialUser } from './social-user';
export abstract class BaseLoginProvider implements LoginProvider {
constructor() {}
readonly changeUser?: EventEmitter<SocialUser>;
abstract initialize(autoLogin?: boolean): Promise<void>;
abstract initialize(autoLogin?: boolean, lang?: string): Promise<void>;
abstract getLoginStatus(): Promise<SocialUser>;
abstract signIn(signInOptions?: object): Promise<SocialUser>;
abstract signOut(revoke?: boolean): Promise<void>;
Expand Down
2 changes: 1 addition & 1 deletion projects/lib/src/entities/login-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SocialUser } from './social-user';

export interface LoginProvider {
readonly changeUser?: EventEmitter<SocialUser>;
initialize(autoLogin?: boolean): Promise<void>;
initialize(autoLogin?: boolean, lang?: string): Promise<void>;
getLoginStatus(): Promise<SocialUser>;
signIn(signInOptions?: object): Promise<SocialUser>;
signOut(revoke?: boolean): Promise<void>;
Expand Down
12 changes: 9 additions & 3 deletions projects/lib/src/providers/google-login-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class GoogleLoginProvider extends BaseLoginProvider {

constructor(
private clientId: string,
private readonly initOptions?: GoogleInitOptions
private readonly initOptions?: GoogleInitOptions,
) {
super();

Expand All @@ -62,12 +62,12 @@ export class GoogleLoginProvider extends BaseLoginProvider {
this._accessToken.pipe(skip(1)).subscribe(this._receivedAccessToken);
}

initialize(autoLogin?: boolean): Promise<void> {
initialize(autoLogin?: boolean, lang?: string): Promise<void> {
return new Promise((resolve, reject) => {
try {
this.loadScript(
GoogleLoginProvider.PROVIDER_ID,
'https://accounts.google.com/gsi/client',
this.getGoogleLoginScriptSrc(lang),
() => {
google.accounts.id.initialize({
client_id: this.clientId,
Expand Down Expand Up @@ -216,4 +216,10 @@ export class GoogleLoginProvider extends BaseLoginProvider {
);
return JSON.parse(jsonPayload);
}

private getGoogleLoginScriptSrc(lang: string): string {
return lang ?
`https://accounts.google.com/gsi/client?hl=${lang}` :
'https://accounts.google.com/gsi/client';
}
}
5 changes: 4 additions & 1 deletion projects/lib/src/socialauth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { GoogleLoginProvider } from './providers/google-login-provider';
*/
export interface SocialAuthServiceConfig {
autoLogin?: boolean;
lang?: string;
providers: { id: string; provider: LoginProvider | Type<LoginProvider> }[];
onError?: (error: any) => any;
}
Expand All @@ -34,6 +35,7 @@ export class SocialAuthService {

private providers: Map<string, LoginProvider> = new Map();
private autoLogin = false;
private lang: string = '';

private _user: SocialUser | null = null;
private _authState: ReplaySubject<SocialUser | null> = new ReplaySubject(1);
Expand Down Expand Up @@ -72,6 +74,7 @@ export class SocialAuthService {

private initialize(config: SocialAuthServiceConfig) {
this.autoLogin = config.autoLogin !== undefined ? config.autoLogin : false;
this.lang = config.lang !== undefined ? config.lang : '';
const { onError = console.error } = config;

config.providers.forEach((item) => {
Expand All @@ -85,7 +88,7 @@ export class SocialAuthService {

Promise.all(
Array.from(this.providers.values()).map((provider) =>
provider.initialize(this.autoLogin)
provider.initialize(this.autoLogin, this.lang),
)
)
.then(() => {
Expand Down

0 comments on commit 3726f52

Please sign in to comment.