Skip to content

Commit

Permalink
Merge pull request #79 from mahdiramezanii/develop
Browse files Browse the repository at this point in the history
change Authentication in project
  • Loading branch information
mahdiramezanii authored May 10, 2024
2 parents 0ec8afe + 0d797a2 commit c1b3577
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 14 deletions.
19 changes: 15 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
import 'package:apple_shop/bloc/authentication/auth_bloc.dart';
import 'package:apple_shop/data/models/bucket_model.dart';
import 'package:apple_shop/di/service_locator.dart';
import 'package:apple_shop/screan/bootom_navigation.dart';
import 'package:apple_shop/screan/login_screan.dart';
import 'package:apple_shop/util/auth_manager.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hive_flutter/hive_flutter.dart';

void main() async {
GlobalKey<NavigatorState> globalKey=GlobalKey<NavigatorState>();

void main() async {
WidgetsFlutterBinding();
await Hive.initFlutter();
Hive.registerAdapter(BucketAdapter());
await Hive.openBox<Bucket>("BucketBox");
await initLocator();
runApp(MyApp());

}

class MyApp extends StatelessWidget {
MyApp({super.key});

MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey:globalKey,
debugShowCheckedModeBanner: false,
home: BottomNavigatonScrean(),
home: (AuthManager.isLoggin())
? BottomNavigatonScrean()
: BlocProvider(
create: (context) => AuthBloc(),
child: LoginScrean(),
),
);
}
}
23 changes: 13 additions & 10 deletions lib/screan/login_screan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "package:apple_shop/bloc/authentication/auth_bloc.dart";
import "package:apple_shop/bloc/authentication/auth_event.dart";
import "package:apple_shop/bloc/authentication/auth_state.dart";
import "package:apple_shop/constants/colors.dart";
import "package:dartz/dartz.dart";

import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:flutter_bloc/flutter_bloc.dart";
Expand All @@ -15,6 +15,7 @@ class LoginScrean extends StatelessWidget {

@override
Widget build(BuildContext context) {

return Directionality(
textDirection: TextDirection.rtl,
child: Scaffold(
Expand Down Expand Up @@ -124,23 +125,25 @@ class LoginScrean extends StatelessWidget {
return const CircularProgressIndicator();
}

if (state is ResponseAuthState){

Text widget=Text("");
if (state is ResponseAuthState) {
Text widget = const Text("");

state.response.fold((l) {
widget=Text(l,style: TextStyle(color: Colors.red),);

widget = Text(
l,
style: const TextStyle(color: Colors.red),
);
}, (r) {

widget=Text(r,style: TextStyle(color: Colors.blue),);

widget = Text(
r,
style: const TextStyle(color: Colors.blue),
);
});

return widget;
}

return Text("خطا");
return const Text("خطا");
}),
],
),
Expand Down
50 changes: 50 additions & 0 deletions lib/screan/profile_screan.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import "package:apple_shop/bloc/authentication/auth_bloc.dart";
import "package:apple_shop/bloc/authentication/auth_state.dart";
import "package:apple_shop/constants/colors.dart";
import "package:apple_shop/main.dart";
import "package:apple_shop/screan/bootom_navigation.dart";
import "package:apple_shop/screan/login_screan.dart";
import "package:apple_shop/util/auth_manager.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:flutter_bloc/flutter_bloc.dart";

class ProfileScrean extends StatelessWidget {
@override
Expand Down Expand Up @@ -279,6 +286,49 @@ class ProfileScrean extends StatelessWidget {
),
),
const Spacer(),
ElevatedButton(
onPressed: () {
AuthManager.logout();
Navigator.of(context)
.pushReplacement(MaterialPageRoute(builder: (context) {
return BlocProvider(
create: (context) {
var bloc = AuthBloc();

bloc.stream.forEach((state) {
if (state is ResponseAuthState) {
state.response.fold((l) => null, (r) {
return globalKey.currentState?.pushReplacement(
MaterialPageRoute(
builder: (context) {
return BottomNavigatonScrean();
},
),
);
});
}
});

return bloc;
},
child: LoginScrean(),
);
}));
},
style: ElevatedButton.styleFrom(
backgroundColor: MyColors.red,
),
child: const Text(
"خروج از حساب کابری",
style: TextStyle(
color: Colors.white,
fontFamily: "sb",
),
),
),
const SizedBox(
height: 40,
),
const Text(
"اپل شاپ",
style: TextStyle(
Expand Down
9 changes: 9 additions & 0 deletions lib/util/auth_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ class AuthManager {
_sharedpref.clear();
changeAuth.value=null;
}

static bool isLoggin(){

String token=get_token();

return token.isNotEmpty;


}
}
14 changes: 14 additions & 0 deletions lib/util/dio_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:apple_shop/util/auth_manager.dart';
import 'package:dio/dio.dart';

class DioProvider {
Dio crateDio() {
Dio dio =
Dio(BaseOptions(baseUrl: "https://startflutter.ir/api/", headers: {
"Content-Type": "application/json",
"Authorization": "Bearer ${AuthManager.get_token()}"
}));

return dio;
}
}

0 comments on commit c1b3577

Please sign in to comment.