Skip to content

Commit

Permalink
Improvements and bug fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicola Lanzilotto committed Oct 16, 2023
1 parent 925fb77 commit 137117e
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 69 deletions.
74 changes: 65 additions & 9 deletions src/app/auth/autologin/autologin.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { AppConfigService } from '../../services/app-config.service';
import { environment } from '../../../environments/environment';
import { ProjectService } from '../../services/project.service';
import { LoggerService } from '../../services/logger/logger.service';
import { LocalDbService } from 'app/services/users-local-db.service';

@Component({
selector: 'appdashboard-autologin',
Expand All @@ -26,8 +27,7 @@ export class AutologinComponent implements OnInit {
FCMcurrentToken: string;
user: any;
public version: string = environment.VERSION;


public hasSignedInWithGoogle: boolean = false;

constructor(
private route: ActivatedRoute,
Expand All @@ -36,7 +36,8 @@ export class AutologinComponent implements OnInit {
public sso: SsoService,
public appConfigService: AppConfigService,
private projectService: ProjectService,
private logger: LoggerService
private logger: LoggerService,
private localDbService: LocalDbService
) {

this.user = auth.user_bs.value;
Expand All @@ -59,6 +60,15 @@ export class AutologinComponent implements OnInit {

}


ngOnInit() {
this.logger.log('[AUTOLOGIN] SSO - autologin page');
this.detectMobile();

}



getJWTAndRouteParamsAndLogin() {
this.route.params.subscribe((params) => {
this.logger.log('[AUTOLOGIN] SSO - autologin page params ', params)
Expand Down Expand Up @@ -96,11 +106,7 @@ export class AutologinComponent implements OnInit {
});
}

ngOnInit() {

this.logger.log('[AUTOLOGIN] SSO - autologin page');
this.detectMobile();
}


detectMobile() {
Expand Down Expand Up @@ -135,10 +141,10 @@ export class AutologinComponent implements OnInit {
}

this.sso.getCurrentAuthenticatedUser(JWT).subscribe(auth_user => {
this.logger.log('[AUTOLOGIN] SSO - ssoLogin getCurrentAuthenticatedUser RES ', auth_user);
// console.log('[AUTOLOGIN] SSO - ssoLogin getCurrentAuthenticatedUser RES ', auth_user);

const user = { firstname: auth_user['firstname'], lastname: auth_user['lastname'], _id: auth_user['_id'], email: auth_user['email'], emailverified: auth_user['emailverified'], token: JWT }
this.logger.log('[AUTOLOGIN] SSO - ssoLogin getCurrentAuthenticatedUser user ', user);
// console.log('[AUTOLOGIN] SSO - ssoLogin getCurrentAuthenticatedUser user ', user);

localStorage.setItem('user', JSON.stringify(user));
localStorage.setItem(chatPrefix + '__tiledeskToken', JWT);
Expand All @@ -147,6 +153,14 @@ export class AutologinComponent implements OnInit {

this.router.navigate([route]);

// get if user has used Signin with Google
const hasSigninWithGoogle = this.localDbService.getFromStorage('swg')
if ( hasSigninWithGoogle ) {
this.localDbService.removeFromStorage('swg')
// console.log('[AUTOLOGIN] SSO removeFromStorage swg')
this.trackUserHasSignedInWithGoogle(user)
}

this.logger.log('[AUTOLOGIN] SSO - ssoLogin JWT before to get permsission ', JWT)
this.logger.log('[AUTOLOGIN] SSO - ssoLogin storedJWT before to get permsission ', storedJWT)
if (JWT !== storedJWT) {
Expand Down Expand Up @@ -179,6 +193,48 @@ export class AutologinComponent implements OnInit {
});
}

trackUserHasSignedInWithGoogle(user) {
if (!isDevMode()) {
if (window['analytics']) {
// try {
// window['analytics'].page("Auth Page, Sign in with Google", {

// });
// } catch (err) {
// this.logger.error('Sign in with Google page error', err);
// }

let userFullname = ''
if (user.firstname && user.lastname) {
userFullname = user.firstname + ' ' + user.lastname
} else if (user.firstname && !user.lastname) {
userFullname = user.firstname
}

try {
window['analytics'].identify(user._id, {
name: userFullname,
email: user.email,
logins: 5,

});
} catch (err) {
this.logger.error('identify Sign in with Google event error', err);
}
// Segments
try {
window['analytics'].track('Signed In', {
"username": userFullname,
"userId": user._id,
'method': "Sign in with Google"
});
} catch (err) {
this.logger.error('track Sign in with Google event error', err);
}
}
}
}

getProjectFromRemotePublishAndSaveInStorage(project_id) {

this.projectService.getProjects().subscribe((prjcts: any) => {
Expand Down
2 changes: 1 addition & 1 deletion src/app/auth/signin/signin.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ <h2>

<div class="login-container">

<button type="button" class="google-button" (click)="signinWithGoogle()">
<button type="button" rel="opener" class="google-button" (click)="signinWithGoogle()">
<div style="margin-right: 10px; background: rgb(255, 255, 255); padding: 10px; border-radius: 2px;">

<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 186.69 190.5"
Expand Down
12 changes: 9 additions & 3 deletions src/app/auth/signin/signin.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ export class SigninComponent implements OnInit {
this.buildForm();
this.getWindowWidthAndHeight();
this.getStoredRoute()
// get if user has used Signin with Google
const hasSigninWithGoogle = this.localDbService.getFromStorage('swg')
if (hasSigninWithGoogle) {
this.localDbService.removeFromStorage('swg')
// console.log('[SIGN-IN] removeFromStorage swg')
}
}

getStoredRoute() {
Expand Down Expand Up @@ -289,7 +295,7 @@ export class SigninComponent implements OnInit {
}

let userFullname = ''
if (user.firstname && user.lastname) {
if (user.firstname && user.lastname) {
userFullname = user.firstname + ' ' + user.lastname
} else if (user.firstname && !user.lastname) {
userFullname = user.firstname
Expand Down Expand Up @@ -445,8 +451,8 @@ export class SigninComponent implements OnInit {
// }
goToSignupPage() {
const storedUser = localStorage.getItem('user');
this.logger.log('[SIGN-IN] GO TO SIGNUP PAGE STORED USER ',storedUser)
if(storedUser) {
this.logger.log('[SIGN-IN] GO TO SIGNUP PAGE STORED USER ', storedUser)
if (storedUser) {
// localStorage.removeItem('user')
this.auth.signOut('signin');
}
Expand Down
12 changes: 9 additions & 3 deletions src/app/auth/signup/signup.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,13 @@ export class SignupComponent implements OnInit, AfterViewInit {
this.buildForm();
this.getBrowserLang();
this.getOSCODE();
this.getQueryParamsAndSegmentRecordPageAndIdentify()
//
this.getQueryParamsAndSegmentRecordPageAndIdentify();

const hasSigninWithGoogle = this.localDbService.getFromStorage('swg')
if (hasSigninWithGoogle) {
this.localDbService.removeFromStorage('swg')
console.log('[SIGN-UP] removeFromStorage swg')
}

}

Expand Down Expand Up @@ -569,7 +574,8 @@ export class SignupComponent implements OnInit, AfterViewInit {
"last_name": signupResponse.user.lastname,
"email": signupResponse.user.email,
"username": userFullname,
'userId': signupResponse.user._id
'userId': signupResponse.user._id,
'method': "Email and Password"
});
} catch (err) {
this.logger.error('track signup event error', err);
Expand Down
10 changes: 10 additions & 0 deletions src/app/core/auth.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,16 @@ export class AuthGuard implements CanActivate {
const decodeCurrentUrl = decodeURIComponent(url)
// console.log('[AUTH-GUARD] _decodeCurrentUrl ', decodeCurrentUrl)

if (decodeCurrentUrl === '/projects') {
// get if user has used Signin with Google
const hasSigninWithGoogle = this.localDbService.getFromStorage('swg')
if (hasSigninWithGoogle) {
this.localDbService.removeFromStorage('swg')
// console.log('[AUTH-GUARD] removeFromStorage swg')
}
}


const storedRoute = this.localDbService.getFromStorage('wannago')
// console.log('[AUTH-GUARD] storedRoute getFromStorage ', storedRoute)

Expand Down
49 changes: 5 additions & 44 deletions src/app/core/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class AuthService {
public tilebotSidebarIsOpened: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(null)
public botsSidebarIsOpened: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(null)
public isChromeVerGreaterThan100: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(null)

show_ExpiredSessionPopup: boolean

_user_role: string
Expand Down Expand Up @@ -254,6 +255,7 @@ export class AuthService {
// --------------------------------------------------------------------------------------
publishSSOloggedUser() {
const storedUser = localStorage.getItem('user')
console.log('[AUTH-SERV] publishSSOloggedUser storedUser ', storedUser)
if (storedUser !== null) {
this.user_bs.next(JSON.parse(storedUser))

Expand Down Expand Up @@ -1332,54 +1334,13 @@ export class AuthService {


public siginWithGoogle() {

console.log('[AUTH-SERV] siginWithGoogle HERE YES!!!')
// const url = this.SERVER_BASE_PATH + "auth/google"
const url = "https://eu.rtmv3.tiledesk.com/api/auth/google"
window.open(url, '_self');

let storedUser = localStorage.getItem('user')
let user = JSON.parse(storedUser);

if (!isDevMode()) {
if (window['analytics']) {
try {
window['analytics'].page("Auth Page, Sign in with Google", {

});
} catch (err) {
this.logger.error('Sign in with Google page error', err);
}

let userFullname = ''
if (user.firstname && user.lastname) {
userFullname = user.firstname + ' ' + user.lastname
} else if (user.firstname && !user.lastname) {
userFullname = user.firstname
}

try {
window['analytics'].identify(user._id, {
name: userFullname,
email: user.email,
logins: 5,

});
} catch (err) {
this.logger.error('identify Sign in with Google event error', err);
}
// Segments
try {
window['analytics'].track('Signed In', {
"username": userFullname,
"userId": user._id,
'method': "Sign in with Google"
});
} catch (err) {
this.logger.error('track Sign in with Google event error', err);
}
}
}

this.localDbService.setInStorage('swg', 'true')

}

public siginUpWithGoogle() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,14 @@ export class CreateProjectGsComponent implements OnInit {
}

try {
window['analytics'].track("Signed Up with Google ", {
window['analytics'].track("Signed Up", {
"type": "organic",
"first_name": user.firstname,
"last_name": user.lastname,
"email": user.email,
"username": userFullname,
'userId': user._id
'userId': user._id,
'method': "Signup up with Google"
});
} catch (err) {
this.logger.error('track signup event error', err);
Expand Down
12 changes: 10 additions & 2 deletions src/app/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ export class HomeComponent implements OnInit, OnDestroy, AfterViewInit {
private logger: LoggerService,
private projectService: ProjectService,
public appStoreService: AppStoreService,
private departmentService: DepartmentService
private departmentService: DepartmentService,
public localDbService: LocalDbService
) {
const brand = brandService.getBrand();
this.company_name = brand['company_name'];
Expand Down Expand Up @@ -263,6 +264,13 @@ export class HomeComponent implements OnInit, OnDestroy, AfterViewInit {
this.waWizardSteps = [{ step1: false, step2: false, step3: false }]
this.oneStepWizard = { watsAppConnected: false }

// get if user has used Signin with Google
const hasSigninWithGoogle = this.localDbService.getFromStorage('swg')
if (hasSigninWithGoogle) {
this.localDbService.removeFromStorage('swg')
// console.log('[SIGN-UP] removeFromStorage swg')
}

}

ngAfterViewInit() {
Expand Down Expand Up @@ -2039,7 +2047,7 @@ export class HomeComponent implements OnInit, OnDestroy, AfterViewInit {
takeUntil(this.unsubscribe$)
)
.subscribe((user) => {
this.logger.log('[HOME] - USER GET IN HOME ', user)
// console.log('[HOME] - USER GET IN HOME ', user)
// tslint:disable-next-line:no-debugger
// debugger
this.user = user;
Expand Down
Loading

0 comments on commit 137117e

Please sign in to comment.