File size: 2,663 Bytes
907b200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

namespace App\Services;

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class UserService
{
    protected $otpService;

    public function __construct(OtpService $otpService)
    {
        $this->otpService = $otpService;
    }

    public function registerUser(array $data): User
    {
        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'role' => 'normal',
            'is_verified' => false,
        ]);

        $this->otpService->resendOtp($user, 'email_verification');

        return $user;
    }

    public function loginUser(array $credentials): array
    {
        $user = User::where('email', $credentials['email'])->first();

        if (!$user || !Hash::check($credentials['password'], $user->password)) {
            return ['error' => 'Invalid email or password', 'code' => 401];
        }

        if (!$user->is_verified) {
            $otpCheck = $this->otpService->canResendOtp($user, 'email_verification');

            if ($otpCheck['can_resend']) {
                $this->otpService->resendOtp($user, 'email_verification');
            }

            return ['error' => 'Email not verified. OTP sent again.', 'code' => 403];
        }

        $user->ensureHasPlan();

        $token = $user->createToken('auth_token')->plainTextToken;

        return ['user' => $user, 'token' => $token];
    }

    public function loginGoogleUser(string $accessToken): array
    {
        $response = \Illuminate\Support\Facades\Http::withToken($accessToken)
            ->get('https://www.googleapis.com/oauth2/v3/userinfo');

        if (!$response->ok()) {
            return ['error' => 'Invalid Google token', 'code' => 401];
        }

        $googleUser = $response->json();

        $user = User::updateOrCreate(
            ['email' => $googleUser['email']],
            [
                'name' => $googleUser['name'] ?? '',
                'password' => Hash::make(Str::random(32)),
                'role' => 'normal',
                'is_verified' => true,
                'photo' => $googleUser['picture'] ?? null,
            ]
        );

        $user->ensureHasPlan();

        $token = $user->createToken('auth_token')->plainTextToken;

        return ['user' => $user, 'token' => $token];
    }

    public function resetPassword(User $user, string $otp, string $newPassword): bool
    {
        return $this->otpService->verifyOtp($user, 'password_reset', $otp) &&
            tap($user, fn($u) => $u->update(['password' => Hash::make($newPassword)]));
    }
}