-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from KosukeSaigusa/45_create_oauth
#45 GoogleとAppleでのログイン処理作成
- Loading branch information
Showing
11 changed files
with
326 additions
and
28 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
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
50 changes: 50 additions & 0 deletions
50
packages/mottai_flutter_app/lib/auth/ui/auth_controller.dart
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,50 @@ | ||
import 'package:flutter/services.dart'; | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
|
||
import '../../../scaffold_messenger_controller.dart'; | ||
import '../auth.dart'; | ||
|
||
final authControllerProvider = Provider.autoDispose<AuthController>( | ||
(ref) => AuthController( | ||
authService: ref.watch(authServiceProvider), | ||
appScaffoldMessengerController: | ||
ref.watch(appScaffoldMessengerControllerProvider), | ||
), | ||
); | ||
|
||
class AuthController { | ||
const AuthController({ | ||
required AuthService authService, | ||
required AppScaffoldMessengerController appScaffoldMessengerController, | ||
}) : _authService = authService, | ||
_appScaffoldMessengerController = appScaffoldMessengerController; | ||
|
||
final AuthService _authService; | ||
final AppScaffoldMessengerController _appScaffoldMessengerController; | ||
|
||
/// 選択した [SignInMethod] でサインインする。 | ||
Future<void> signIn(SignInMethod authenticator) async { | ||
switch (authenticator) { | ||
case SignInMethod.google: | ||
try { | ||
await _authService.signInWithGoogle(); | ||
} | ||
// キャンセル時 | ||
on PlatformException catch (e) { | ||
if (e.code == 'network_error') { | ||
_appScaffoldMessengerController | ||
.showSnackBar('接続できませんでした。\nネットワーク状況を確認してください。'); | ||
} | ||
_appScaffoldMessengerController.showSnackBar('キャンセルしました。'); | ||
} | ||
|
||
case SignInMethod.apple: | ||
// Apple はキャンセルやネットワークエラーの判定ができないので、try-catchしない | ||
await _authService.signInWithApple(); | ||
case SignInMethod.line: | ||
case SignInMethod.email: | ||
throw UnimplementedError(); | ||
} | ||
return; | ||
} | ||
} |
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,46 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
import 'package:sign_in_button/sign_in_button.dart'; | ||
|
||
import '../auth.dart'; | ||
import 'auth_controller.dart'; | ||
|
||
class GoogleAppleSignin extends ConsumerWidget { | ||
const GoogleAppleSignin({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context, WidgetRef ref) { | ||
return Scaffold( | ||
appBar: AppBar( | ||
title: const Text('サインインページ'), | ||
), | ||
body: Center( | ||
child: Column( | ||
children: [ | ||
SizedBox( | ||
height: 50, | ||
child: SignInButton( | ||
Buttons.google, | ||
text: 'Google でサインイン', | ||
onPressed: () async => ref | ||
.read(authControllerProvider) | ||
.signIn(SignInMethod.google), | ||
), | ||
), | ||
// Apple | ||
SizedBox( | ||
height: 50, | ||
child: SignInButton( | ||
Buttons.apple, | ||
text: 'Apple でサインイン', | ||
onPressed: () async => | ||
ref.read(authControllerProvider).signIn(SignInMethod.apple), | ||
), | ||
), | ||
], | ||
), | ||
), | ||
); | ||
} | ||
} |
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.