aboutsummaryrefslogtreecommitdiffstats
path: root/source/app/Http/Controllers/ForgotPasswordController.php
blob: 6b3cc6648216131a12152238fb147a0fe025e0ef (plain) (blame)
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
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Student;
use App\Models\Company;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class ForgotPasswordController extends Controller
{
   public function showLinkRequestForm()
    {
        return view('student.register.email');
    }

    public function sendResetLinkEmail(Request $request)
    {
        // Valideer het e-mailadres
        $request->validate([
            'email' => 'required|email'
        ]);

        $email = $request->email;
        $id = null;

        // Zoek student of bedrijf
        $student = Student::where('email', $email)->first();
        if ($student) {
            $id = $student->id;
        } else {
            $company = Company::where('email', $email)->first();
            if ($company) {
                $id = $company->id;
            } else {
                return back()->withErrors([
                    'email' => 'Dit e-mailadres hoort niet bij een geregistreerde student of bedrijf.'
                ]);
            }
        }

        // Probeer herstelmail te versturen
        try {
            $url = "http://10.2.160.208/api/reset/mail/{$id}";
            Log::info("Versturen van herstelmail naar ID: $id via $url");

            $response = Http::timeout(10)->post($url);

            if ($response->successful()) {
                return back()->with('status', 'Als dit e-mailadres bij ons bekend is, ontvang je een herstelmail.');
            } else {
                Log::error("Herstelmail fout: " . $response->body());
                return back()->withErrors([
                    'email' => 'Er ging iets mis bij het versturen van de herstelmail.'
                ]);
            }
        } catch (\Exception $e) {
            Log::error("Fout bij resetmail: " . $e->getMessage());
            return back()->withErrors([
                'email' => 'Serverfout bij het versturen van de herstelmail. Probeer het later opnieuw.'
            ]);
        }
    }
}