Skip to content

Commit

Permalink
feat(oauth login): can now sign in and up with microsoft oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
sambrus committed Dec 10, 2024
1 parent e7a82d3 commit 2abdd22
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 26 deletions.
1 change: 0 additions & 1 deletion client_mobile/lib/data/workflow.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:client_mobile/data/action.dart';
import 'package:flutter/gestures.dart';

class WorkflowEvent
{
Expand Down
24 changes: 22 additions & 2 deletions client_mobile/lib/pages/auth/login.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,28 @@ class _LoginPageState extends State<LoginPage> {
Align(
alignment: Alignment.center,
child: SignInButton(
onPressed: () {
MicrosoftAuthService.auth(context);
onPressed: () async {
bool isRegistered = await MicrosoftAuthService.auth(
context,
signUp: true);
if (isRegistered) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Microsoft link avec succès !"),
backgroundColor: Colors.black,
),
);
context.pushReplacement("/dashboard");
}
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Microsoft authentification failed."),
backgroundColor: Colors.red,
),
);
}
},
label: "Sign in with Microsoft",
image: Image.asset(
Expand Down
32 changes: 29 additions & 3 deletions client_mobile/lib/pages/auth/register.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,13 @@ class _RegisterPageState extends State<RegisterPage> {
label: "Register",
onPressed: () async {
if (_formKey.currentState!.validate()) {
bool isRegistered = await AuthService.register(context, RegisterObject(email: emailController.text, password: passwordController.text, username: userController.text).toJson());
bool isRegistered = await AuthService.register(
context,
RegisterObject(
email: emailController.text,
password: passwordController.text,
username: userController.text)
.toJson());
if (isRegistered) {
context.pushReplacement("/dashboard");
}
Expand All @@ -122,8 +128,28 @@ class _RegisterPageState extends State<RegisterPage> {
Align(
alignment: Alignment.center,
child: SignInButton(
onPressed: () {
MicrosoftAuthService.auth(context);
onPressed: () async {
bool isRegistered = await MicrosoftAuthService.auth(
context,
signUp: true);
if (isRegistered) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Microsoft link avec succès !"),
backgroundColor: Colors.black,
),
);
context.pushReplacement("/dashboard");
}
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Microsoft authentification failed."),
backgroundColor: Colors.red,
),
);
}
},
label: "Sign in with Microsoft",
image: Image.asset(
Expand Down
17 changes: 16 additions & 1 deletion client_mobile/lib/pages/dashboard/service_connection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,22 @@ class _ServiceConnectionPageState extends State<ServiceConnectionPage> {
if (microsoftToken == null)
SignInButton(
onPressed: () async {
MicrosoftAuthService.auth(context);
bool isLinked = await MicrosoftAuthService.auth(context);
if (isLinked) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Microsoft link avec succès !"),
backgroundColor: Colors.black,
),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Microsoft authentification failed."),
backgroundColor: Colors.red,
),
);
}
await _loadMicrosoftToken();
},
label: "Link with Microsoft",
Expand Down
2 changes: 1 addition & 1 deletion client_mobile/lib/pages/dashboard/workflow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class _WorkflowPageState extends State<WorkflowPage> {
Navigator.of(context).pop({
"name": nameController.text,
"description": descriptionController.text,
}); // Retourne les données
});
},
child: const Text("Save"),
),
Expand Down
32 changes: 14 additions & 18 deletions client_mobile/lib/services/microsoft/microsoft_auth_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,25 @@ class MicrosoftAuthService {
static const String authority = "https://login.microsoftonline.com/common";
static const FlutterSecureStorage secureStorage = FlutterSecureStorage();

static Future<bool> linkToken(String token) async {
static Future<bool> linkToken(String token, {bool signUp = false}) async {
String baseUrl = dotenv.env["BACKEND_BASE_URL"] ?? "http://localhost:8080";
final response = await http.post(
Uri.parse('$baseUrl/oauth/microsoft'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({"token": token}),
);

print("code retourné : ${response.statusCode}");
// dans ce cas là on link le jwt recu avec notre bearer_token
if (signUp) {
final responseData = jsonDecode(response.body);
await secureStorage.write(
key: 'bearer_token', value: responseData["jwt"]);
}

return response.statusCode == 200;
}

static Future<void> auth(BuildContext context) async {
static Future<bool> auth(BuildContext context, {bool signUp = false}) async {
final Config config = Config(
tenant: "common",
clientId: clientId,
Expand All @@ -46,28 +52,18 @@ class MicrosoftAuthService {
),
),
(r) async {
bool hasLinked = await linkToken(r.accessToken!);
bool hasLinked = await linkToken(r.accessToken!, signUp: signUp);
print("access token microsoft = : ${r.accessToken}");

if (hasLinked) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Microsoft link avec succès !"),
backgroundColor: Colors.black,
),
);
await secureStorage.write(
key: 'microsoft_access_token', value: r.accessToken);
return (true);
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text("Microsoft authentification failed."),
backgroundColor: Colors.red,
),
);
return (false);
}

print("access token = : ${r.accessToken}");
},
);
return (true);
}
}

0 comments on commit 2abdd22

Please sign in to comment.