File size: 1,266 Bytes
52d9fb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

namespace App\Http\Controllers\User;

use App\Models\ApiKey;
use App\Constants\Status;
use App\Http\Controllers\Controller;

class ApiKeyController extends Controller
{

    public function index()
    {
        $pageTitle = 'Api Documentation';
        $user      = auth()->user();

        if (!$user->is_author) {
            $notify[] = ['error', 'Only author can allow to access'];
            return back()->withNotify($notify);
        }

        $apiKey = ApiKey::where('user_id', auth()->id())->where('status', Status::ACTIVE_KEY)->first();
        return view('Template::user.api_key', compact('pageTitle', 'apiKey'));
    }

    public function keyGenerate()
    {
        $user      = auth()->user();
        $latestKey = ApiKey::where('user_id', $user->id)->where('status', Status::ACTIVE_KEY)->first();

        if (@$latestKey) {
            $latestKey->status = Status::INACTIVE_KEY;
            $latestKey->save();
        }

        $newKey          = new ApiKey();
        $newKey->user_id = $user->id;
        $newKey->key     = generateApiKey();
        $newKey->status  = Status::ACTIVE_KEY;
        $newKey->save();

        $notify[] = ['success', 'API key generated successfully'];
        return back()->withNotify($notify);
    }
}