100 lines
3.0 KiB
PHP
100 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class UserController extends Controller
|
|
{
|
|
public function showProfile()
|
|
{
|
|
return view('user-profile');
|
|
}
|
|
|
|
public function update_profile(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'name' => ['required', 'min:3', 'max:225'],
|
|
'username' => ['required', 'min:3', 'max:10'],
|
|
'email' => 'required|email',
|
|
], [
|
|
'required' => ':attribute harus diisi.',
|
|
'email' => ':attribute harus berupa email yang valid.',
|
|
'min' => 'panjang :attribute minimal :min karakter.',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return redirect('user-profile')
|
|
->withErrors($validator)
|
|
->withInput();
|
|
}
|
|
|
|
$user = User::findOrFail(Auth::user()->id);
|
|
$user->update([
|
|
'name' => $request->name,
|
|
'username' => $request->username,
|
|
'email' => $request->email,
|
|
]);
|
|
return redirect('user-profile')->with('success', 'Profile Berhasil Diperbarui!');
|
|
}
|
|
|
|
public function updatePassword(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'password' => 'required|min:5|max:255',
|
|
'new_password' => 'required|min:5|max:255',
|
|
], [
|
|
'required' => ':attribute harus diisi.',
|
|
'min' => 'panjang :attribute minimal :min karakter.',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return redirect('user-profile')
|
|
->withErrors($validator)
|
|
->withInput();
|
|
}
|
|
|
|
$user = User::findOrFail(Auth::user()->id);
|
|
$user->update([
|
|
'password' => $request->new_password,
|
|
]);
|
|
return redirect('user-profile')->with('success', 'Password Berhasil Diperbarui!');
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$user = Auth::user();
|
|
return view('user-profile', compact('user'));
|
|
}
|
|
|
|
public function updateProfilePicture(Request $request)
|
|
{
|
|
$request->validate([
|
|
'profile_picture' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
|
|
]);
|
|
|
|
$user = Auth::user();
|
|
|
|
// menghapus foto profil lama jika ada
|
|
if ($user->profile_picture) {
|
|
Storage::disk('public')->delete($user->profile_picture);
|
|
}
|
|
|
|
// Menyimpan file gambar yang diunggah oleh pengguna dan memperbarui gambar profil
|
|
if ($request->hasFile('profile_picture')) {
|
|
$imagePath = $request->file('profile_picture')->store('profile_pictures', 'public');
|
|
$user->profile_picture = $imagePath;
|
|
$user->save();
|
|
}
|
|
|
|
return redirect()->route('user-profile')->with('success', 'Foto Profile Berhasil Diperbarui!');
|
|
}
|
|
|
|
}
|