import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'package:jwt_decoder/jwt_decoder.dart'; import 'package:flutter_jwt_auth_example/exceptions/form_exceptions.dart'; import 'package:flutter_jwt_auth_example/exceptions/secure_storage_exceptions.dart'; import 'package:flutter_jwt_auth_example/model/user_model.dart'; import 'package:flutter_jwt_auth_example/services/helper_service.dart'; import 'package:flutter_jwt_auth_example/services/secure_storage_service.dart'; class AuthService { static const String loginPath = 'token/'; static const String registerPath = 'users/'; static const String refreshPath = 'token/refresh/'; static const String verifyPath = 'token/verify/'; static Future loadUser() async { final json = await SecureStorageService.storage.read( key: SecureStorageService.userKey, ); if (json != null) { return User.fromJson(jsonDecode(json)); } else { throw SecureStorageNotFoundException(); } } static void saveUser(User user) async { await SecureStorageService.storage.write( key: SecureStorageService.userKey, value: user.toJson(), ); } static Future refreshToken(User user) async { final response = await http.post( HelperService.buildUri(refreshPath), headers: HelperService.buildHeaders(), body: jsonEncode( { 'refresh': user.refreshToken, }, ), ); final statusType = (response.statusCode / 100).floor() * 100; switch (statusType) { case 200: final json = jsonDecode(response.body); user.accessToken = json['access']; saveUser(user); break; case 400: case 300: case 500: default: throw Exception('Error contacting the server!'); } } static Future register({ required String email, required String password, required String cellphone, required String firstName, required String lastName, }) async { final response = await http.post( HelperService.buildUri(loginPath), headers: HelperService.buildHeaders(), body: jsonEncode( { 'email': email, 'password': password, 'cellphone': cellphone, 'first_name': firstName, 'last_name': lastName }, ), ); final statusType = (response.statusCode / 100).floor() * 100; switch (statusType) { case 200: final json = jsonDecode(response.body); final user = User.fromJson(json); saveUser(user); return user; case 400: final json = jsonDecode(response.body); throw handleFormErrors(json); case 300: case 500: default: throw FormGeneralException(message: 'Error contacting the server!'); } } static Future logout() async { await SecureStorageService.storage.delete( key: SecureStorageService.userKey, ); } static Future login({ required String email, required String password, }) async { final response = await http.post( HelperService.buildUri(loginPath), headers: HelperService.buildHeaders(), body: jsonEncode( { 'email': email, 'password': password, }, ), ); final statusType = (response.statusCode / 100).floor() * 100; switch (statusType) { case 200: final json = jsonDecode(response.body); final user = User.fromJson(json); saveUser(user); return user; case 400: final json = jsonDecode(response.body); throw handleFormErrors(json); case 300: case 500: default: throw FormGeneralException(message: 'Error contacting the server!'); } } }