Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
6.3 kB
4
Indexable
Never

class UserController extends Controller
{

    // fungsi untuk register akun
    function register(Request $request)
    {
        try {
            $request->validate([
                'email' => 'required|email|unique:users,email',
                'nama' => 'required',
                'password' => 'required',
            ]);

            $data = $request->all();
            $data['password'] = md5($request->password);
            $data['role'] = 'masyarakat';

            User::create($data);
            return ResponseFormatter::success(['status' => 'Register Berhasil'], 'Register Berhasil. Silahkan login');
        } catch (ValidationException $e) {
            $errors = $e->errors();

            $emailError = isset($errors['email']) ? $errors['email'][0] : '';

            if (!empty($emailError)) {
                return ResponseFormatter::error([
                    'message' => 'Validasi Error',
                    'error' => $e->getMessage(),
                ], $emailError, 200);
            }

            return ResponseFormatter::error([
                'message' => 'Validasi Error',
                'error' => $e->getMessage(),
            ], $emailError, 200);
        } catch (Exception $error) {
            return ResponseFormatter::error([
                'message' => 'Gagal !',
                'error' => $error->getMessage(),
            ], 'Register Gagal', 500);
        }
    }

    // fungsi untuk login
    function auth(Request $request)
    {
        try {
            $request->validate([
                'email' => 'required',
                'password' => 'required',
            ]);

            $password = md5($request->password);
            if (!Auth::attempt(['email' => $request->email, 'password' => $password])) {
                return ResponseFormatter::error([
                    'message' => 'Login gagal, periksa Email dan password'
                ], 'Login gagal, periksa Email dan password', 200);
            }

            $user = User::where('email', $request->email)->first();
            if ($user->role == 'admin') {
                Auth::logout();
                return ResponseFormatter::error([
                    'message' => 'Login gagal, periksa Email dan password'
                ], 'Login gagal, periksa Email dan password', 200);
            }

            $tokenResult = $user->createToken('authToken')->plainTextToken;
            return ResponseFormatter::success([
                'access_token' => $tokenResult,
                'token_type' => 'Bearer',
                'user' => $user
            ], 'Login berhasil');
        } catch (Exception $error) {
            return ResponseFormatter::error([
                'message' => 'Gagal !',
                'error' => $error->getMessage(),
            ], 'Login Gagal', 500);
        }
    }

    // fungsi untuk cek data user
    function user(Request $request)
    {
        return ResponseFormatter::success(['user' => $request->user()], 'Profil ditemukan');
    }

	// fungsi untuk edit data akun
    function editAkun(Request $request)
    {
        try {
            if (Auth::user()->role == 'masyarakat') {
                $request->validate([
                    'email' => 'required|email|unique:user,email,' . Auth::user()->id . ',id',
                    'nama' => 'required',
                ]);
            } else {
                $request->validate([
                    'email' => 'required|email|unique:user,email,' . Auth::user()->id . ',id',
                    'nama' => 'required',
                ]);
            }


            $data = $request->all();

            User::where('id', Auth::user()->id)->update($data);
            $user = User::find(Auth::user()->id);
            return ResponseFormatter::success(['status' => 'Berhasil perbarui data', 'user' => $user], 'Berhasil perbarui data');
        } catch (ValidationException $e) {
            $errors = $e->errors();

            $emailError = isset($errors['email']) ? $errors['email'][0] : '';

            if (!empty($emailError)) {
                return ResponseFormatter::error([
                    'message' => 'Validasi Error',
                    'error' => $e->getMessage(),
                ], $emailError, 200);
            }

            return ResponseFormatter::error([
                'message' => 'Validasi Error',
                'error' => $e->getMessage(),
            ], $emailError, 200);
        } catch (Exception $error) {
            return ResponseFormatter::error([
                'message' => 'Gagal !',
                'error' => $error->getMessage(),
            ], 'Gagal', 500);
        }
    }

// fungsi untuk ganti password    
function editPassword(Request $request)
    {
        try {
            // cek apakah Nomor KK sudah terdaftar
            $akun = User::select('password')
                ->find(Auth::user()->id);

            if (!Hash::check(md5($request->password), $akun->password)) {
                return ResponseFormatter::error([
                    'status' => 'Password yang anda masukkan salah!'
                ], 'Gagal, Password yang anda masukkan salah!', 200);
            }

            $data['password'] = bcrypt(md5($request->password_baru));
            User::where('id', Auth::user()->id)->update($data);

            return ResponseFormatter::success([
                'message' => 'Berhasil perbarui password'
            ], 'Berhasil perbarui password');
        } catch (Exception $e) {
            return ResponseFormatter::error([
                'message' => 'Gagal meperbarui password, coba lagi!',
                'error' =>  $e->getMessage()
            ], 'Gagal meperbarui password, coba lagi!', 200);
        }
    }

// fungsi untuk logout
    public function logout(Request $request)
    {
        try {
            $token = $request->user()->currentAccessToken()->delete();
            return ResponseFormatter::success(['status' => 'Logout Berhasil'], 'Logout Berhasil');
        } catch (Exception $error) {
            return ResponseFormatter::error(
                [
                    'message' => 'Gagal !',
                    'error' => $error->getMessage(),
                ],
                'Logout Gagal',
                500
            );
        }
    }
}
Leave a Comment