diff --git a/package-lock.json b/package-lock.json index 30655bb..86e98ef 100644 --- a/package-lock.json +++ b/package-lock.json @@ -42,6 +42,7 @@ "diff-match-patch-ts": "^0.6.0", "font-awesome": "^4.7.0", "install": "^0.13.0", + "jwt-decode": "^4.0.0", "lodash-es": "^4.17.21", "mat-progress-buttons": "^9.3.1", "ngx-cron-editor": "^0.8.1", @@ -8907,6 +8908,15 @@ "safe-buffer": "^5.0.1" } }, + "node_modules/jwt-decode": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/jwt-decode/-/jwt-decode-4.0.0.tgz", + "integrity": "sha512-+KJGIyHgkGuIq3IEBNftfhW/LfWhXUIY6OmyVWjliu5KH1y0fw7VQ8YndE2O4qZdMSd9SqbnC8GOcZEy0Om7sA==", + "license": "MIT", + "engines": { + "node": ">=18" + } + }, "node_modules/karma": { "version": "6.4.3", "resolved": "https://registry.npmjs.org/karma/-/karma-6.4.3.tgz", @@ -20454,6 +20464,11 @@ "safe-buffer": "^5.0.1" } }, + "jwt-decode": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/jwt-decode/-/jwt-decode-4.0.0.tgz", + "integrity": "sha512-+KJGIyHgkGuIq3IEBNftfhW/LfWhXUIY6OmyVWjliu5KH1y0fw7VQ8YndE2O4qZdMSd9SqbnC8GOcZEy0Om7sA==" + }, "karma": { "version": "6.4.3", "resolved": "https://registry.npmjs.org/karma/-/karma-6.4.3.tgz", diff --git a/package.json b/package.json index fa0f231..88e8413 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ }, "scripts": { "ng": "ng", - "start": "ng serve --host 0.0.0.0 --port 4200 --disable-host-check", + "start": "ng serve --proxy-config proxy.conf.json", "startnp": "ng serve --proxy-config=./proxy.conf.ts --host 0.0.0.0", "build": "ng build", "watch": "ng build --watch --configuration development", @@ -52,6 +52,7 @@ "diff-match-patch-ts": "^0.6.0", "font-awesome": "^4.7.0", "install": "^0.13.0", + "jwt-decode": "^4.0.0", "lodash-es": "^4.17.21", "mat-progress-buttons": "^9.3.1", "ngx-cron-editor": "^0.8.1", diff --git a/proxy.conf.json b/proxy.conf.json index fbff922..38adad1 100644 --- a/proxy.conf.json +++ b/proxy.conf.json @@ -1,6 +1,6 @@ { "/api": { - "target": "http://192.168.1.26:3000/", + "target": "http://localhost:3000", "secure": false, "changeOrigin": true, "logLevel": "debug", diff --git a/src/app/providers/mikrowizard/data.ts b/src/app/providers/mikrowizard/data.ts index 8c77fbc..f1d51f5 100644 --- a/src/app/providers/mikrowizard/data.ts +++ b/src/app/providers/mikrowizard/data.ts @@ -10,7 +10,7 @@ import { User } from './user'; export class dataProvider { // public serverUrl: string = "/api"; - public serverUrl: string = "http://192.168.1.26:3000"; + public serverUrl: string = "http://localhost:3000"; private db: string = "NothingImportant"; private apiUrl: string = "/api"; @@ -652,12 +652,9 @@ export class dataProvider { } } - async loginWithApple(appleResponse: any): Promise { + async singSignonLoginForUser(data: any): Promise { try { - const data = { - appleResponse: appleResponse - }; - return this.MikroWizardRPC.sendJsonRequest("/api/auth/apple", data); + return this.MikroWizardRPC.sendJsonRequest("/api/single-signon/user/create", data); } catch (error) { throw error; } diff --git a/src/app/views/pages/login/login.component.ts b/src/app/views/pages/login/login.component.ts index 2929e51..b60b74d 100644 --- a/src/app/views/pages/login/login.component.ts +++ b/src/app/views/pages/login/login.component.ts @@ -7,6 +7,7 @@ import { MsalService } from '@azure/msal-angular'; import { loginRequest } from '../../../auth/msal-config'; import { appleConfig } from '../../../auth/apple-config'; import appleSignin from 'apple-signin-auth'; +import { MikroWizardUtils } from '../../../../components/utils/common-functions'; declare global { interface Window { @@ -35,6 +36,7 @@ export class LoginComponent implements OnInit { private data_provider: dataProvider, private login_checker: loginChecker, private msalService: MsalService + ) { this.createForm(); }; @@ -42,7 +44,7 @@ export class LoginComponent implements OnInit { ngOnInit() { // Check if user is already logged in with MSAL if (this.msalService.instance.getActiveAccount()) { - this.handleMsalLogin(); + // this.handleMsalLogin(); } // Initialize Apple Sign In @@ -89,7 +91,8 @@ export class LoginComponent implements OnInit { this.msalService.loginPopup(loginRequest) .subscribe({ next: (result) => { - this.handleMsalLogin(); + console.log(result) + this.handleMsalLogin(result?.account); }, error: (error) => { this.error_msg = "Error during Office 365 login: " + error.message; @@ -98,10 +101,16 @@ export class LoginComponent implements OnInit { }); } - private handleMsalLogin() { - const account = this.msalService.instance.getActiveAccount(); - if (account) { - this.data_provider.loginWithOffice365(account.idTokenClaims) + private handleMsalLogin(request: any) { + if (request) { + let data = { + email:request?.username, + username:request?.username, + first_name:request?.name?.split(" ")[0], + last_name:request?.name?.split(" ")[1] + } + console.log(">>>>>>>>>>>>>>> data from token >>>>>>", data) + this.data_provider.singSignonLoginForUser(data) .then(res => { if ('uid' in res && res['uid']) { this.error_msg = ""; @@ -152,7 +161,7 @@ export class LoginComponent implements OnInit { private handleAppleLogin(response: any) { console.log(JSON.stringify(response)); - this.data_provider.loginWithApple(response) + this.data_provider.singSignonLoginForUser(response) .then(res => { if ('uid' in res && res['uid']) { this.error_msg = ""; diff --git a/src/components/utils/common-functions.ts b/src/components/utils/common-functions.ts new file mode 100644 index 0000000..9e75914 --- /dev/null +++ b/src/components/utils/common-functions.ts @@ -0,0 +1,27 @@ +// your.component.ts or service.ts +import { Injectable } from '@angular/core'; +import {jwtDecode} from 'jwt-decode'; + +interface JwtPayload { + // Define keys you expect from the JWT payload + sub?: string; + name?: string; + email?: string; + exp?: number; + [key: string]: any; // to allow additional keys +} + +export class MikroWizardUtils { + + public static decodeJWT(token: any) { + try { + const decoded = jwtDecode(token); + console.log('Decoded JWT:', decoded); + console.log('User email:', decoded['unique_name']); + return decoded; + } catch (error) { + console.error('Invalid JWT token:', error); + return null; + } + } +}