Line data Source code
1 : import 'dart:io';
2 :
3 : import 'package:app_pym/core/error/exceptions.dart';
4 : import 'package:flutter/foundation.dart';
5 : import 'package:http/http.dart' as http;
6 : import 'package:injectable/injectable.dart';
7 : import 'package:shared_preferences/shared_preferences.dart';
8 :
9 : abstract class AuthenticationService {
10 : /// Return [token] if logged in. Return [null] if not.
11 : String get token;
12 :
13 : /// Send email reset
14 : Future<void> forgotPassword(String email);
15 :
16 : /// Send email verification
17 : Future<void> sendEmailVerification();
18 :
19 : Future<String> signIn(String email, String password);
20 : void signOut();
21 : Future<String> signUp(String email, String password);
22 : }
23 :
24 : @prod
25 : @LazySingleton(as: AuthenticationService)
26 : class AuthenticationServiceImpl implements AuthenticationService {
27 : final http.Client client;
28 : final SharedPreferences prefs;
29 :
30 1 : AuthenticationServiceImpl({
31 : @required this.client,
32 : @required this.prefs,
33 : });
34 1 : @override
35 1 : String get token => _token;
36 :
37 3 : String get _token => prefs.getString('token');
38 :
39 3 : set _token(String token) => prefs.setString('token', token);
40 :
41 : @override
42 1 : Future<void> forgotPassword(String email) async {
43 1 : final data = <String, String>{
44 : 'email': email,
45 : };
46 :
47 3 : final response = await client.post(
48 : 'https://admin.map-pym.com/auth/forgot_password',
49 : body: data,
50 : );
51 :
52 2 : if (response.statusCode == HttpStatus.ok) {
53 : return;
54 : } else {
55 1 : throw ServerException(
56 3 : 'Forget Password Failed : ${response.statusCode}, ${response.reasonPhrase}');
57 : }
58 : }
59 :
60 : @override
61 1 : Future<void> sendEmailVerification() async {
62 3 : final response = await client.post(
63 : 'https://admin.map-pym.com/api/auth/email_verification',
64 3 : headers: {HttpHeaders.authorizationHeader: "Bearer $_token"},
65 : );
66 :
67 2 : if (response.statusCode == HttpStatus.ok) {
68 : return;
69 : } else {
70 1 : throw ServerException(
71 3 : 'Email Verification Failed : ${response.statusCode}, ${response.reasonPhrase}');
72 : }
73 : }
74 :
75 : @override
76 1 : Future<String> signIn(String email, String password) async {
77 : // Sign out
78 1 : _token = null;
79 :
80 1 : final data = <String, String>{
81 : 'email': email,
82 : 'password': password,
83 : };
84 3 : final response = await client.post(
85 : 'https://admin.map-pym.com/api/auth/login',
86 : body: data,
87 : );
88 :
89 2 : if (response.statusCode == HttpStatus.ok) {
90 2 : return _token = response.body;
91 : } else {
92 1 : throw ServerException(
93 3 : 'Cannot log in : ${response.statusCode}, ${response.reasonPhrase}');
94 : }
95 : }
96 :
97 1 : @override
98 : void signOut() {
99 2 : client.post(
100 : 'https://admin.map-pym.com/api/auth/logout',
101 3 : headers: {HttpHeaders.authorizationHeader: "Bearer $_token"},
102 2 : ).catchError((dynamic e) {
103 2 : print("CAUGHT $e"); // Ignore errors
104 : });
105 1 : _token = null;
106 : }
107 :
108 : @override
109 1 : Future<String> signUp(String email, String password) async {
110 1 : _token = null;
111 1 : final data = <String, String>{
112 : 'email': email,
113 : 'password': password,
114 : };
115 3 : final response = await client.post(
116 : 'https://admin.map-pym.com/api/auth/register',
117 : body: data,
118 : );
119 :
120 2 : if (response.statusCode == HttpStatus.ok) {
121 2 : return _token = response.body;
122 : } else {
123 1 : throw ServerException(
124 3 : 'Cannot register : ${response.statusCode}, ${response.reasonPhrase}');
125 : }
126 : }
127 : }
|