Spaces:
Sleeping
Sleeping
File size: 5,875 Bytes
14459e2 |
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
import 'dart:convert';
import 'package:scientry_api/commons/models/user.dart';
import 'package:scientry_api/service/email_manager.dart';
import 'package:scientry_api/service/gsheets_manager.dart';
import 'package:scientry_api/service/jwt_manager.dart';
import 'package:shelf/shelf.dart';
class EmailVerificationHandler {
GsheetsManager gsheetsManager = GsheetsManager();
EmailManager emailManager = EmailManager();
Future<Response> verifyEmail(Request request) async {
try {
// Extract the token from the query parameters
final token = request.url.queryParameters['token'];
if (token == null || token.isEmpty) {
return Response.badRequest(
body: jsonEncode({
"errorCode": -1,
"errorMessage": "Missing or invalid token.",
}),
headers: {'Content-Type': 'application/json'},
);
}
String? isVerified = JWTManager.verifyEmailVerificationToken(token);
if (isVerified == null) {
return Response.forbidden(
jsonEncode({
"errorCode": -1,
"errorMessage": "Invalid or expired token.",
}),
headers: {'Content-Type': 'application/json'},
);
} else if (isVerified.contains("@")) {
UserModel? user = await gsheetsManager.getUserByField(
field: "email",
value: isVerified,
);
if (user == null) {
return Response.notFound(
jsonEncode({"errorCode": -1, "errorMessage": "User not found."}),
headers: {'Content-Type': 'application/json'},
);
} else if (user.isEmailVerified == true) {
// If the user is already verified
return Response.ok(
jsonEncode({
"errorCode": 0,
"errorMessage": "Email is already verified.",
}),
headers: {'Content-Type': 'application/json'},
);
} else {
// Update the user's verification status
final updatedUser = user.copyWith(
isEmailVerified: true,
updatedAt: DateTime.now().millisecondsSinceEpoch,
);
print("UserData: ${updatedUser.toJson()}");
bool status = await gsheetsManager.addUpdateUser(updatedUser);
if (!status) {
return Response.internalServerError(
body: jsonEncode({
"errorCode": -1,
"errorMessage": "Failed to update user.",
}),
headers: {'Content-Type': 'application/json'},
);
} else {
// Successfully verified the email
return Response.ok(
jsonEncode({
"errorCode": 0,
"errorMessage": "Email verified successfully.",
}),
headers: {'Content-Type': 'application/json'},
);
}
}
} else {
return Response.forbidden(
jsonEncode({
"errorCode": -1,
"errorMessage": "Invalid token format.",
}),
headers: {'Content-Type': 'application/json'},
);
}
} catch (e) {
return Response.internalServerError(
body: jsonEncode({
"errorCode": -1,
"errorMessage": "An error occurred: ${e.toString()}",
}),
headers: {'Content-Type': 'application/json'},
);
}
}
Future<Response> requestEmailVerification(Request request) async {
try {
final body = await request.readAsString();
final data = jsonDecode(body);
String? email = data['email'];
if (email == null || email.isEmpty) {
return Response.badRequest(
body: jsonEncode({
"errorCode": -1,
"errorMessage": "Email is required.",
}),
headers: {'Content-Type': 'application/json'},
);
}
UserModel? user = await gsheetsManager.getUserByField(
field: "email",
value: email,
);
if (user == null) {
return Response.notFound(
jsonEncode({"errorCode": -1, "errorMessage": "User not found."}),
headers: {'Content-Type': 'application/json'},
);
} else if (user.isEmailVerified == true) {
return Response.ok(
jsonEncode({
"errorCode": 0,
"errorMessage": "Email is already verified.",
}),
headers: {'Content-Type': 'application/json'},
);
} else if (user.isDeleted == true) {
return Response.notFound(
jsonEncode({"errorCode": -1, "errorMessage": "User not found."}),
headers: {'Content-Type': 'application/json'},
);
} else {
bool status = await emailManager.sendVerificationEmail(
name: user.name!,
email: user.email!,
);
if (!status) {
return Response.internalServerError(
body: jsonEncode({
"errorCode": -1,
"errorMessage": "Failed to send verification email.",
}),
headers: {'Content-Type': 'application/json'},
);
} else {
return Response.ok(
jsonEncode({
"errorCode": 0,
"errorMessage": "Verification email sent successfully.",
}),
headers: {'Content-Type': 'application/json'},
);
}
}
} catch (e) {
return Response.internalServerError(
body: jsonEncode({
"errorCode": -1,
"errorMessage": "An error occurred: ${e.toString()}",
}),
headers: {'Content-Type': 'application/json'},
);
}
}
}
|