forked from RivaanRanawat/flutter-google-docs-clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth_repository.dart
111 lines (101 loc) · 3.18 KB
/
auth_repository.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import 'dart:convert';
import 'package:docs_clone_flutter/constants.dart';
import 'package:docs_clone_flutter/models/error_model.dart';
import 'package:docs_clone_flutter/models/user_model.dart';
import 'package:docs_clone_flutter/repository/local_storage_repository.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:http/http.dart';
final authRepositoryProvider = Provider(
(ref) => AuthRepository(
googleSignIn: GoogleSignIn(),
client: Client(),
localStorageRepository: LocalStorageRepository(),
),
);
final userProvider = StateProvider<UserModel?>((ref) => null);
class AuthRepository {
final GoogleSignIn _googleSignIn;
final Client _client;
final LocalStorageRepository _localStorageRepository;
AuthRepository({
required GoogleSignIn googleSignIn,
required Client client,
required LocalStorageRepository localStorageRepository,
}) : _googleSignIn = googleSignIn,
_client = client,
_localStorageRepository = localStorageRepository;
Future<ErrorModel> signInWithGoogle() async {
ErrorModel error = ErrorModel(
error: 'Some unexpected error occurred.',
data: null,
);
try {
final user = await _googleSignIn.signIn();
if (user != null) {
final userAcc = UserModel(
email: user.email,
name: user.displayName ?? '',
profilePic: user.photoUrl ?? '',
uid: '',
token: '',
);
var res = await _client.post(Uri.parse('$host/api/signup'), body: userAcc.toJson(), headers: {
'Content-Type': 'application/json; charset=UTF-8',
});
switch (res.statusCode) {
case 200:
final newUser = userAcc.copyWith(
uid: jsonDecode(res.body)['user']['_id'],
token: jsonDecode(res.body)['token'],
);
error = ErrorModel(error: null, data: newUser);
_localStorageRepository.setToken(newUser.token);
break;
}
}
} catch (e) {
error = ErrorModel(
error: e.toString(),
data: null,
);
}
return error;
}
Future<ErrorModel> getUserData() async {
ErrorModel error = ErrorModel(
error: 'Some unexpected error occurred.',
data: null,
);
try {
String? token = await _localStorageRepository.getToken();
if (token != null) {
var res = await _client.get(Uri.parse('$host/'), headers: {
'Content-Type': 'application/json; charset=UTF-8',
'x-auth-token': token,
});
switch (res.statusCode) {
case 200:
final newUser = UserModel.fromJson(
jsonEncode(
jsonDecode(res.body)['user'],
),
).copyWith(token: token);
error = ErrorModel(error: null, data: newUser);
_localStorageRepository.setToken(newUser.token);
break;
}
}
} catch (e) {
error = ErrorModel(
error: e.toString(),
data: null,
);
}
return error;
}
void signOut() async {
await _googleSignIn.signOut();
_localStorageRepository.setToken('');
}
}