Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit profile #78

Merged
merged 13 commits into from
Apr 26, 2020
2 changes: 1 addition & 1 deletion .github/workflows/laravel.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Feature Tests
name: Laravel

on: [push]

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Node
name: npm

on: [push]

Expand Down
95 changes: 95 additions & 0 deletions app/Http/Controllers/Api/EmailVerificationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\VerifiesEmails;

use App\User;

class EmailVerificationController extends Controller
{
/*
|--------------------------------------------------------------------------
| Email Verification Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling email verification for any
| user that recently registered with the application. Emails may also
| be re-sent if the user didn't receive the original email message.
|
*/

// use VerifiesEmails;

/**
* Where to redirect users after verification.
*
* @var string
*/
protected $redirectTo = '/home';

/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth:api')->only('resend');
$this->middleware('signed')->only('verify');
$this->middleware('throttle:6,1')->only('verify', 'resend');
}

/**
* Mark the authenticated user's email address as verified.
*
* @param \Illuminate\Http\Request $request
* @return json response
*
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function verify(Request $request)
{
$user = User::find($request->route('id'));

if (! hash_equals((string) $request->route('id'), (string) $user->getKey())) {
throw new AuthorizationException;
}

if (! hash_equals((string) $request->route('hash'), sha1($user->getEmailForVerification()))) {
throw new AuthorizationException;
}

if ($user->hasVerifiedEmail()) {
return response()->json(['message' => 'Email already verified'], 403);
}

$user->markEmailAsVerified();
// if ($user->markEmailAsVerified()) {
// event(new Verified($user));
// }

return response()->json(['message' => 'Successfully verified email.']);
}

/**
* Resend the email verification notification.
*
* @param \Illuminate\Http\Request $request
* @return json response
*/
public function resend(Request $request)
{
$user = User::find($request->user()->id);

if ($user->hasVerifiedEmail()) {
return response()->json(['message' => 'Email is already verified.']);
}

$user->sendEmailVerificationNotification();

return response()->json(['message' => 'Email verification sent.']);
}
}
9 changes: 6 additions & 3 deletions app/Http/Controllers/Api/FriendListController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

class FriendListController extends Controller
{
protected $withSender = 'sender:id,first_name,middle_name,last_name,email';
protected $withReceiver = 'receiver:id,first_name,middle_name,last_name,email';

/**
* Fetch all friend list which status is friends
* Notice: This fetches the current user friends only
Expand All @@ -27,7 +30,7 @@ public function index(Request $request){
$query->where('user_one', $authID)
->orWhere('user_two', $authID);
})
->with('sender:id,name,email', 'receiver:id,name,email')
->with($this->withSender, $this->withReceiver)
->paginate();

return response()->json($data);
Expand All @@ -42,7 +45,7 @@ public function pendingReceivedRequests(Request $request)
$data = $request->user()->friendReceived()
->select('id', 'user_one', 'created_at')
->where('status', 'pending')
->with('sender:id,name,email')
->with($this->withSender)
->orderBy('id', 'desc')
->paginate();

Expand All @@ -65,7 +68,7 @@ public function pendingSentRequests(Request $request)
$data = $request->user()->friendSent()
->select('id', 'user_two', 'created_at')
->where('status', 'pending')
->with('receiver:id,name,email')
->with($this->withReceiver)
->orderBy('id', 'desc')
->paginate();

Expand Down
8 changes: 6 additions & 2 deletions app/Http/Controllers/Api/MessageThreadsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

class MessageThreadsController extends Controller
{
protected $withSender = 'sender:id,first_name,middle_name,last_name';
protected $withReceiver = 'receiver:id,first_name,middle_name,last_name';

/**
* Display the most recent conversations of the authenticated user.
*
Expand All @@ -21,7 +24,7 @@ public function index()

$data = MessageThread::where('user_one', $authenticatedUserId)
->orWhere('user_two', $authenticatedUserId)
->with('sender:id,name', 'receiver:id,name')
->with($this->withSender, $this->withReceiver)
->orderBy('last_activity', 'asc')
->paginate();

Expand Down Expand Up @@ -56,7 +59,8 @@ public function store(Request $request)
*/
public function show($id)
{
$data = MessageThread::with('sender:id,name','receiver:id,name')->find($id);
$data = MessageThread::with($this->withSender, $this->withReceiver)
->find($id);

// If thread does not exist, return 404
if(!$data)
Expand Down
6 changes: 4 additions & 2 deletions app/Http/Controllers/Api/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ public function searchUserSpecific(SearchUser $data)
$toSearch = $data->validated()['q'];
$toSearch = strtolower($toSearch);

$data = User::select('id','name','created_at')
->where('name','like','%'.$toSearch.'%')
$data = User::select('id','first_name','last_name','created_at')
->where('first_name','like','%'.$toSearch.'%')
->orWhere('middle_name','like','%'.$toSearch.'%')
->orWhere('last_name','like','%'.$toSearch.'%')
->orWhere('email','like','%'.$toSearch.'%')
->paginate();

Expand Down
88 changes: 77 additions & 11 deletions app/Http/Controllers/Api/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
use Auth;

use App\User;
use App\Traits\PasswordMatchCheck;
use App\Http\Requests\UserRegistration;
use App\Http\Requests\UserLogin;
use App\Http\Requests\UserUpdateDetails;
use App\Http\Requests\UserChangeEmail;
use App\Http\Requests\UserChangePassword;

class UserController extends Controller
{
use PasswordMatchCheck;

/**
* Display a listing of the resource.
Expand All @@ -27,15 +32,15 @@ public function index()
}

/**
* Show a specified resource
*
* @param \Illuminate\Http\Request $request
* Show a specified resource
*
* @param $userId - I.D of a user
* @return User::class
*/
public function show($userId)
{
$data = User::find($userId);

if($data){
return response()->json($data);
}
Expand All @@ -45,6 +50,7 @@ public function show($userId)

/**
* Register user, store the resource to database
*
* @param App\Http\Requests\UserRegistration $request
* @return \Illuminate\Http\Response
*/
Expand All @@ -61,13 +67,17 @@ public function store(UserRegistration $request)
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @param App\Http\Request\UserUpdateDetails $request
* @param int $id - I.D of a user
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
public function update(UserUpdateDetails $request, $id)
{
//
$user = User::findOrFail($id);

$user->update($request->validated());

return response()->json($user);
}

/**
Expand All @@ -81,6 +91,12 @@ public function destroy($id)
//
}

/**
* Attempt to login/authenticate a user.
*
* @param App\Http\Request\UserLogin $request
* @return \Illuminate\Http\Response
*/
public function login(UserLogin $request){
$data = $request->validated();

Expand All @@ -90,19 +106,69 @@ public function login(UserLogin $request){

return response()->json(['message' => 'Successful Authentication', 'access_token' => $access_token, 'user' => $user], 200);
}

return response()->json(['message' => 'Invalid Credentials'], 404);

}

/**
* Return the details of the authenticated user
*
*
* @return \Illuminate\Http\Response
*/
public function authDetails(){
$data = request()->user();
$data->full_name = $data->fullName();

return response()->json($data, 200);
}

/**
* Change/Update the email of the authenticated user
*
* @param \App\Http\Requests\ChangeEmail $request
* @return \Illuminate\Http\Response
*/
public function changeEmail(UserChangeEmail $request)
{
$user = $request->user();
$data = $request->validated();
$updateData = [
'email' => $data['email'],
'email_verified_at' => null
];

// Check if password is correct
$this->passwordMatchCheck($user->password, $data['password']);

$user->update($updateData);

// Email a verification link to the new email
$req = Request::create(route('verification.resend'), 'GET');
$ret = app()->handle($req);

return response()->json([
'message' => 'A mail has been sent to verify the new email. Please check your inbox.',
'user' => $user,
]);
}

/**
* Change the password of the user
*
* @param \App\Http|Requests\UserChangePassword $request
* @return \Illuminate\Http|Response
*/
public function changePassword(UserChangePassword $request)
{
$user = $request->user();
$this->passwordMatchCheck($user->password, $request->old_password);

$newPassword = $request->only('password');
$newPassword['password'] = Hash::make($newPassword['password']);

$user->update($newPassword);

return response()->json(['message' => 'Password successfuly changed.']);
}
}
32 changes: 32 additions & 0 deletions app/Http/Requests/UserChangeEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UserChangeEmail extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'email' => 'required|unique:users,email',
'email_confirmation' => 'required|same:email',
'password' => 'required|between:6,32'
];
}
}
Loading