From 9290f6ef7645a7385d55b4deb50fab6c77032602 Mon Sep 17 00:00:00 2001 From: Quentin Challon Date: Sun, 14 Jan 2024 21:22:32 +0100 Subject: [PATCH 1/3] Change something --- api/src/auth/auth.controller.ts | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/api/src/auth/auth.controller.ts b/api/src/auth/auth.controller.ts index 90724bd..c30f75b 100644 --- a/api/src/auth/auth.controller.ts +++ b/api/src/auth/auth.controller.ts @@ -1,14 +1,24 @@ -import { Body, Controller, Post } from '@nestjs/common'; +import { Body, Controller, Post, Headers, UnauthorizedException } from '@nestjs/common'; import { AuthService } from './auth.service'; import { SignInDto } from './_utils/dto/request/sign-in.dto'; import { CreateUserDto } from '../users/_utils/dto/request/create-user.dto'; -import { ApiTags } from '@nestjs/swagger'; +import { ApiHeader, ApiTags } from '@nestjs/swagger'; import { GoogleLoginDto } from './_utils/dto/request/google-login.dto'; +import { UsersService } from '../users/users.service'; +import * as jwt from 'jsonwebtoken'; +import { ConfigService } from '@nestjs/config'; +import { EnvironmentVariables } from '../_utils/config'; +import { JwtStrategy } from './jwt/jwt.startegy'; @ApiTags('Auth') @Controller('auth') export class AuthController { - constructor(private readonly authService: AuthService) {} + constructor( + private readonly authService: AuthService, + private readonly usersService: UsersService, + private readonly configService: ConfigService, + private readonly jwtStrategy: JwtStrategy, + ) {} /** * Endpoint for user login. @@ -26,7 +36,15 @@ export class AuthController { } @Post('google-login') - googleLogin(@Body() googleLoginDto: GoogleLoginDto) { + async googleLogin(@Headers('Authorization') authorizationHeader: string, @Body() googleLoginDto: GoogleLoginDto) { + if (authorizationHeader) { + const token = authorizationHeader.split(' ')[1]; + const decodeToken = jwt.verify(token, this.configService.get('JWT_SECRET')); + if (!decodeToken) throw new UnauthorizedException('Bad token'); + const user = await this.jwtStrategy.validate(decodeToken); + console.log(user); + return this.usersService.updateGoogleToken(user, googleLoginDto.accessToken); + } return this.authService.connectWithGoogle(googleLoginDto); } } From d8f576145d6a41f38c2569b2d22cabb62d0b46fe Mon Sep 17 00:00:00 2001 From: Valegox Date: Sun, 14 Jan 2024 22:07:23 +0100 Subject: [PATCH 2/3] Fix logout --- flutter_area/lib/UI/Profile/profile_view.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flutter_area/lib/UI/Profile/profile_view.dart b/flutter_area/lib/UI/Profile/profile_view.dart index 17390d2..476284e 100644 --- a/flutter_area/lib/UI/Profile/profile_view.dart +++ b/flutter_area/lib/UI/Profile/profile_view.dart @@ -44,7 +44,7 @@ class _ProfileViewState extends State { onPressed: () async { await userManager.logout(); // ignore: use_build_context_synchronously - Navigator.of(context).pushNamed('/'); + Navigator.of(context).pop(); }) ]), ], From 900b8f3f244875e890163a44e743af166609b1b2 Mon Sep 17 00:00:00 2001 From: Valegox Date: Sun, 14 Jan 2024 22:07:42 +0100 Subject: [PATCH 3/3] Add Google connection --- .../lib/Core/Manager/google_manager.dart | 38 +++++++++++++++++-- .../lib/UI/Connections/connections_view.dart | 18 +++++++-- flutter_area/lib/UI/Login/google_button.dart | 37 +++++++----------- 3 files changed, 62 insertions(+), 31 deletions(-) diff --git a/flutter_area/lib/Core/Manager/google_manager.dart b/flutter_area/lib/Core/Manager/google_manager.dart index 19fe057..5180265 100644 --- a/flutter_area/lib/Core/Manager/google_manager.dart +++ b/flutter_area/lib/Core/Manager/google_manager.dart @@ -1,4 +1,5 @@ import 'dart:convert'; +import 'package:google_sign_in/google_sign_in.dart'; import 'package:http/http.dart' as http; import '../../Utils/constants.dart'; @@ -7,8 +8,22 @@ import '../Locator/locator.dart'; import 'user_manager.dart'; class GoogleManager { + Future openGoogleAuthPopup() async { + const List scopes = [ + 'email', + 'https://www.googleapis.com/auth/gmail.addons.current.action.compose', + 'https://www.googleapis.com/auth/gmail.compose', + ]; + + final GoogleSignIn googleSignIn = GoogleSignIn( + scopes: scopes, + ); + final GoogleSignInAccount? res = await googleSignIn.signIn(); + return res; + } + Future loginWithGoogle( - String accessToken, String completeName, String email) async { + String googleToken, String completeName, String email) async { final http.Response res = await http.post( Uri.parse('$kBaseUrl/auth/google-login'), headers: { @@ -16,7 +31,7 @@ class GoogleManager { 'Content-Type': 'application/json; charset=UTF-8', }, body: jsonEncode({ - 'accessToken': accessToken, + 'accessToken': googleToken, 'completeName': completeName, 'email': email })); @@ -31,6 +46,23 @@ class GoogleManager { return false; } + Future setGoogleToken(String googleToken) async { + final UserManager userManager = locator(); + final http.Response res = + await http.post(Uri.parse('$kBaseUrl/users/google-token'), + headers: { + 'accept': 'application/json', + 'Content-Type': 'application/json; charset=UTF-8', + 'Authorization': 'Bearer ${userManager.accessToken}', + }, + body: jsonEncode({'googleToken': googleToken})); + if (res.statusCode != 201) { + return; + } + userManager.isGoogleLogged = googleToken != 'none'; + userManager.connectionsViewModel?.notify(); + } + Future createDraft(String name, String email) async { try { final UserManager userManager = locator(); @@ -49,6 +81,4 @@ class GoogleManager { } return false; } - - Future signOutFromGoogle() async {} } diff --git a/flutter_area/lib/UI/Connections/connections_view.dart b/flutter_area/lib/UI/Connections/connections_view.dart index 7826b41..ce01ac4 100644 --- a/flutter_area/lib/UI/Connections/connections_view.dart +++ b/flutter_area/lib/UI/Connections/connections_view.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; +import 'package:google_sign_in/google_sign_in.dart'; import 'package:provider/provider.dart'; import '../../Core/Locator/locator.dart'; @@ -96,10 +97,21 @@ class _ConnectionsViewState extends State { ? AppLocalizations.of(context)!.logout : AppLocalizations.of(context)!.connect, onPressed: () async { - if (userManager.isGithubLogged!) { - await googleManager.signOutFromGoogle(); + if (userManager.isGoogleLogged!) { + await googleManager.setGoogleToken('none'); } else { - // todo connect to google + final GoogleSignInAccount? res = + await googleManager.openGoogleAuthPopup(); + if (res == null) { + return; + } + final GoogleSignInAuthentication googleToken = + await res.authentication; + if (googleToken.accessToken == null) { + return; + } + googleManager + .setGoogleToken(googleToken.accessToken!); } }, ), diff --git a/flutter_area/lib/UI/Login/google_button.dart b/flutter_area/lib/UI/Login/google_button.dart index 30af66f..720842d 100644 --- a/flutter_area/lib/UI/Login/google_button.dart +++ b/flutter_area/lib/UI/Login/google_button.dart @@ -6,7 +6,6 @@ import '../../Core/Locator/locator.dart'; import '../../Core/Manager/google_manager.dart'; import '../../Utils/Extensions/color_extensions.dart'; import '../../Utils/Extensions/double_extensions.dart'; -import '../../Utils/mk_print.dart'; class GoogleButton extends StatelessWidget { const GoogleButton({ @@ -16,30 +15,20 @@ class GoogleButton extends StatelessWidget { Future loginWithGoogle(BuildContext context) async { final GoogleManager googleManager = locator(); - const List scopes = [ - 'email', - 'https://www.googleapis.com/auth/gmail.addons.current.action.compose', - 'https://www.googleapis.com/auth/gmail.compose', - ]; - - final GoogleSignIn googleSignIn = GoogleSignIn( - scopes: scopes, - ); + final GoogleSignInAccount? res = await googleManager.openGoogleAuthPopup(); + if (res == null) { + return; + } - try { - final GoogleSignInAccount? res = await googleSignIn.signIn(); - final GoogleSignInAuthentication token = await res!.authentication; - if (token.accessToken == null || res.displayName == null) { - return; - } - final bool success = await googleManager.loginWithGoogle( - token.accessToken!, res.displayName!, res.email); - if (success) { - // ignore: use_build_context_synchronously - Navigator.of(context).pushNamed('/home'); - } - } catch (error) { - mkPrint(error); + final GoogleSignInAuthentication token = await res.authentication; + if (token.accessToken == null || res.displayName == null) { + return; + } + final bool success = await googleManager.loginWithGoogle( + token.accessToken!, res.displayName!, res.email); + if (success) { + // ignore: use_build_context_synchronously + Navigator.of(context).pushNamed('/home'); } }