forked from kookmin-sw/cap-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[FE] auth_service 테스트 완료
- Loading branch information
Showing
7 changed files
with
124 additions
and
89 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class ApiFailResponse { | ||
final bool success; | ||
final String message; | ||
final String code; | ||
|
||
ApiFailResponse({ | ||
required this.success, | ||
required this.message, | ||
required this.code, | ||
}); | ||
|
||
ApiFailResponse.fromJson(Map<String, dynamic> json) | ||
: success = json['success'] as bool, | ||
message = json['message'] as String, | ||
code = json['code'] as String; | ||
} |
This file was deleted.
Oops, something went wrong.
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,16 @@ | ||
class ApiSuccessResponse { | ||
final bool success; | ||
final String message; | ||
final Map<String, dynamic> response; | ||
|
||
ApiSuccessResponse({ | ||
required this.success, | ||
required this.message, | ||
required this.response, | ||
}); | ||
|
||
ApiSuccessResponse.fromJson(Map<String, dynamic> json) | ||
: success = json['success'] as bool, | ||
message = json['message'] as String, | ||
response = json['response']; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import 'dart:convert'; | ||
import 'dart:ui'; | ||
|
||
import 'package:capstone_front/models/api_success_response.dart'; | ||
import 'package:capstone_front/screens/signup/college_department.dart'; | ||
import 'package:capstone_front/screens/signup/signup_email_screen.dart'; | ||
import 'package:capstone_front/screens/signup/signup_util.dart'; | ||
|
@@ -17,6 +19,7 @@ import 'package:provider/provider.dart'; | |
late UserCredential credential; | ||
|
||
Map<String, String> userInfo = { | ||
'uuid': '', | ||
'id': '', | ||
'pw': '', | ||
'pwRe': '', | ||
|
@@ -29,45 +32,33 @@ Map<String, String> userInfo = { | |
int selectedPageIndex = 0; | ||
|
||
Future<String> signup() async { | ||
try { | ||
// UserCredential credential = | ||
// await FirebaseAuth.instance.createUserWithEmailAndPassword( | ||
// email: '${userInfo['id']}@kookmin.ac.kr', | ||
// password: userInfo['pw']!, | ||
// ); | ||
// await credential.user!.sendEmailVerification(); | ||
|
||
// TODO 우리 서버에 signup 요청 여기서 | ||
// signupInfo 에는 실제 유저에게 입력받은 정보가 들어가야함 | ||
|
||
// final Map<String, dynamic> signupInfo = { | ||
// // "uuid": credential.user!.uid, | ||
// "uuid": 'messi', | ||
// "email": '[email protected]', | ||
// "name": 'jihun', | ||
// "country": 'korea', | ||
// "phoneNumber": '010-8276-8291', | ||
// "major": "sw", | ||
// }; | ||
// var response = AuthService.signUp(signupInfo); | ||
|
||
// response의 결과에 따라 성공, 실패, 이유 띄워야함 | ||
Map<String, dynamic> userData = { | ||
'uuid': userInfo['uuid'], | ||
'email': "${userInfo['id']}@kookmin.ac.kr", | ||
'name': userInfo['name'], | ||
'country': userInfo['country'], | ||
'phoneNumber': '010-8276-8291', | ||
'major': userInfo['department'], | ||
}; | ||
|
||
var isSucceed = await AuthService.signUp(userData); | ||
if (isSucceed) { | ||
return 'success'; | ||
} on FirebaseAuthException catch (e) { | ||
return e.code; | ||
} catch (e) { | ||
return e.toString(); | ||
} | ||
throw Exception('there is something problem while signup'); | ||
} | ||
|
||
Future<String> sendEmailAuth(String email, String pw) async { | ||
FlutterSecureStorage storage = const FlutterSecureStorage(); | ||
|
||
try { | ||
credential = await FirebaseAuth.instance.createUserWithEmailAndPassword( | ||
email: '${userInfo['id']}@kookmin.ac.kr', | ||
password: userInfo['pw']!, | ||
); | ||
|
||
userInfo['uuid'] = credential.user!.uid; | ||
await storage.write(key: 'uuid', value: credential.user!.uid); | ||
await credential.user?.sendEmailVerification(); | ||
|
||
return "success"; | ||
|
@@ -77,15 +68,16 @@ Future<String> sendEmailAuth(String email, String pw) async { | |
} | ||
|
||
Future<String> isEmailAuth(String email, String pw) async { | ||
FlutterSecureStorage storage = const FlutterSecureStorage(); | ||
try { | ||
// UserCredential credential = await FirebaseAuth.instance | ||
// .signInWithEmailAndPassword(email: email, password: pw); | ||
User? user = credential.user; | ||
await user!.reload(); | ||
user = FirebaseAuth.instance.currentUser; | ||
|
||
// User? user = credential.user; | ||
// await user!.reload(); | ||
// user = FirebaseAuth.instance.currentUser; | ||
if (credential.user!.emailVerified) { | ||
// user = credential.user; | ||
if (user!.emailVerified) { | ||
user = credential.user; | ||
userInfo['uuid'] = user!.uid; | ||
await storage.write(key: 'uuid', value: user.uid); | ||
return "success"; | ||
} else { | ||
return "email"; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,20 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:capstone_front/models/api_fail_response.dart'; | ||
import 'package:capstone_front/models/api_success_response.dart'; | ||
import 'package:flutter_dotenv/flutter_dotenv.dart'; | ||
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; | ||
import 'package:http/http.dart' as http; | ||
import 'package:crypto/crypto.dart'; | ||
|
||
class AuthService { | ||
static String baseUrl = dotenv.get('BASE_URL'); | ||
static final List<int> key = base64Decode(dotenv.get('HMAC_SECRET')); | ||
|
||
static Future<Map<String, dynamic>> signUp( | ||
Map<String, dynamic> userInfo) async { | ||
static Future<bool> signUp(Map<String, dynamic> userInfo) async { | ||
var hmacSha256 = Hmac(sha256, key); | ||
final url = Uri.parse('$baseUrl/user/signup'); | ||
|
||
var dummy = { | ||
"uuid": "string", | ||
"email": "aㅁ[email protected]", | ||
"name": "string", | ||
"country": "string", | ||
"phoneNumber": "010-8276-8291", | ||
"major": "string" | ||
}; | ||
|
||
// var json = jsonEncode(dummy); | ||
var json = jsonEncode(userInfo); | ||
var bytes = utf8.encode(json); | ||
var digest = hmacSha256.convert(bytes); | ||
|
@@ -37,18 +29,19 @@ class AuthService { | |
body: json, | ||
); | ||
|
||
final String decodedBody = utf8.decode(response.bodyBytes); | ||
final Map<String, dynamic> res = jsonDecode(decodedBody); | ||
|
||
if (response.statusCode != 201) { | ||
final String decodedBody = utf8.decode(response.bodyBytes); | ||
final ApiFailResponse apiFailResponse = jsonDecode(decodedBody); | ||
print('Request failed with status: ${response.statusCode}.'); | ||
print('Request failed with status: ${response.body}'); | ||
print('Request failed with status: ${apiFailResponse.message}'); | ||
return false; | ||
} | ||
|
||
return res; | ||
return true; | ||
} | ||
|
||
static Future<Map<String, dynamic>> signIn(Map<String, dynamic> info) async { | ||
static Future<bool> signIn(Map<String, dynamic> info) async { | ||
FlutterSecureStorage storage = const FlutterSecureStorage(); | ||
var hmacSha256 = Hmac(sha256, key); | ||
final url = Uri.parse('$baseUrl/user/signin'); | ||
|
||
|
@@ -67,35 +60,62 @@ class AuthService { | |
); | ||
|
||
final String decodedBody = utf8.decode(response.bodyBytes); | ||
final Map<String, dynamic> res = jsonDecode(decodedBody); | ||
|
||
if (response.statusCode != 200) { | ||
print('Request failed with status: ${response.statusCode}.'); | ||
print('Request failed with status: ${response.body}'); | ||
final Map<String, dynamic> jsonMap = jsonDecode(decodedBody); | ||
if (response.statusCode == 200) { | ||
final ApiSuccessResponse apiSuccessResponse = | ||
ApiSuccessResponse.fromJson(jsonMap); | ||
await storage.write( | ||
key: 'accessToken', | ||
value: apiSuccessResponse.response['accessToken'], | ||
); | ||
await storage.write( | ||
key: 'refreshToken', | ||
value: apiSuccessResponse.response['refreshToken'], | ||
); | ||
} else { | ||
final ApiFailResponse apiFailResponse = ApiFailResponse.fromJson(jsonMap); | ||
print(response.statusCode); | ||
print(apiFailResponse.message); | ||
throw Exception("fail to get toekns"); | ||
} | ||
|
||
return res; | ||
return true; | ||
} | ||
|
||
static Future<Map<String, dynamic>> reissue( | ||
Map<String, dynamic> refreshTokenObj) async { | ||
static void reissue() async { | ||
FlutterSecureStorage storage = const FlutterSecureStorage(); | ||
|
||
final url = Uri.parse('$baseUrl/auth/reissue'); | ||
final refreshToken = storage.read(key: 'refreshToken'); | ||
|
||
final response = await http.post( | ||
url, | ||
headers: <String, String>{ | ||
'Content-Type': 'application/json; charset=UTF-8', | ||
}, | ||
body: jsonEncode(refreshTokenObj), | ||
body: jsonEncode({ | ||
"refreshToekn": refreshToken, | ||
}), | ||
); | ||
|
||
final String decodedBody = utf8.decode(response.bodyBytes); | ||
final Map<String, dynamic> res = jsonDecode(decodedBody); | ||
|
||
if (response.statusCode != 200) { | ||
print('Request failed with status: ${response.statusCode}.'); | ||
final Map<String, dynamic> jsonMap = jsonDecode(decodedBody); | ||
if (response.statusCode == 200) { | ||
final ApiSuccessResponse apiSuccessResponse = | ||
ApiSuccessResponse.fromJson(jsonMap); | ||
await storage.write( | ||
key: 'accessToken', | ||
value: apiSuccessResponse.response['accessToken'], | ||
); | ||
await storage.write( | ||
key: 'refreshToken', | ||
value: apiSuccessResponse.response['refreshToken'], | ||
); | ||
} else { | ||
final ApiFailResponse apiFailResponse = ApiFailResponse.fromJson(jsonMap); | ||
print(response.statusCode); | ||
print(apiFailResponse.message); | ||
throw Exception("fail to reissue"); | ||
} | ||
|
||
return res; | ||
} | ||
} |