Skip to content

Commit

Permalink
Merge pull request #7 from tomatophp/develop
Browse files Browse the repository at this point in the history
add reset password & notifications
  • Loading branch information
3x1io authored Mar 25, 2024
2 parents 46a0288 + b0f9c18 commit 48e4431
Show file tree
Hide file tree
Showing 23 changed files with 865 additions and 59 deletions.
104 changes: 103 additions & 1 deletion Modules/CircleXO/App/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,36 @@ public function store(Request $request)
return redirect()->route('account.otp');
}

public function resend()
{
if(auth('accounts')->user()){
return redirect()->route('profile.index');
}

if (Session::has('email')){
$account = Account::where('email', Session::get('email'))->first();
if($account){
$account->update([
"otp_code" => rand(100000, 999999)
]);

SendOTP::dispatch(Account::class, $account->id);
AccountRegistered::dispatch(Account::class, $account->id);

Toast::success('OTP code sent successfully!')->autoDismiss(2);
return redirect()->back();
}
else
{
return redirect()->route('account.register');
}
}
else
{
return redirect()->route('account.register');
}
}

public function login()
{
if(auth('accounts')->user()){
Expand Down Expand Up @@ -108,6 +138,79 @@ public function reset()
return view('circle-xo::auth.reset');
}

public function email(Request $request)
{
$request->validate([
"email" => "required|string|email|max:255|exists:accounts,email"
]);

$account = Account::where('email', $request->get('email'))->first();

if($account){
$account->update([
"otp_code" => rand(100000, 999999)
]);

SendOTP::dispatch(Account::class, $account->id);
AccountRegistered::dispatch(Account::class, $account->id);

if (Session::has('email')){
Session::forget('email');
}

Session::put('email', $account->email);

Toast::success('OTP code sent successfully!')->autoDismiss(2);
return redirect()->route('account.password');
}
else {
Toast::danger('Invalid email!')->autoDismiss(2);
return redirect()->back();
}
}

public function password()
{
if(auth('accounts')->user()){
return redirect()->route('profile.index');
}

if (Session::has('email')){
return view('circle-xo::auth.password');
}
else
{
return redirect()->route('account.register');
}
}

public function passwordUpdate(Request $request)
{
$request->validate([
'otp_code' => 'required|string|max:6|exists:accounts,otp_code',
'password' => 'required|string|min:8|confirmed',
]);

$account = Account::where('email', Session::get('email'))->first();

if ($account->otp_code == $request->otp_code){
$account->otp_activated_at = Carbon::now();
$account->otp_code = null;
$account->password = bcrypt($request->get('password'));
$account->save();

Session::forget('email');

Toast::success('Account password has been reset successfully!')->autoDismiss(2);
return redirect()->route('account.login');
}
else
{
Toast::danger('Invalid OTP code!')->autoDismiss(2);
return redirect()->back();
}
}

public function otp()
{
if(auth('accounts')->user()){
Expand Down Expand Up @@ -147,6 +250,5 @@ public function checkOtp(Request $request)
Toast::danger('Invalid OTP code!')->autoDismiss(2);
return redirect()->back();
}

}
}
20 changes: 20 additions & 0 deletions Modules/CircleXO/App/Http/Controllers/ProfileActionsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Modules\CircleXO\App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Models\Account;
use Modules\CircleXO\App\Models\AccountContact;
use Modules\CircleXO\App\Models\AccountListing;
use Modules\TomatoNotifications\App\Models\NotificationsTemplate;
use Modules\TomatoNotifications\App\Models\UserNotification;
use Modules\TomatoNotifications\App\Models\UserToken;
use ProtoneMedia\Splade\Facades\Toast;

class ProfileActionsController extends Controller
{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

namespace Modules\CircleXO\App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Models\Account;
use Modules\CircleXO\App\Models\AccountContact;
use Modules\CircleXO\App\Models\AccountListing;
use Modules\TomatoNotifications\App\Models\NotificationsTemplate;
use Modules\TomatoNotifications\App\Models\UserNotification;
use Modules\TomatoNotifications\App\Models\UserToken;
use ProtoneMedia\Splade\Facades\Toast;

class ProfileNotificationsController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$notification = UserNotification::where('model_type',config('tomato-crm.model'))
->where('model_id', auth('accounts')->user()->id)
->orWhere('model_id', null)
->orderBy('id', 'desc')
->paginate(5);

foreach ($notification as $item) {
$item->date = $item->created_at->diffForHumans();
if ($item->template_id) {
$template = NotificationsTemplate::find($item->template_id);
$item->image = count($template->getMedia('image')) ? $template->getMedia('image')->first()->getUrl() : url('images/default.png');
}
}
return view('circle-xo::notifications.index', [
"notifications" => $notification
]);
}

public function show(UserNotification $model)
{
if(auth('accounts')->user()->id === $model->model_id){
$model->read();
$model->date = $model->created_at->diffForHumans();
if ($model->template_id) {
$template = NotificationsTemplate::find($model->template_id);
$model->image = count($template->getMedia('image')) ? $template->getMedia('image')->first()->getUrl() : url('images/default.png');
}
return view('circle-xo::notifications.show', [
"model" => $model
]);
}
else {
return redirect()->back();
}
}

public function clearUser(): \Illuminate\Http\RedirectResponse
{
UserNotification::where('model_type',config('tomato-crm.model'))
->where('model_id', auth('accounts')->user()->id)
->orderBy('id', 'desc')
->take(10)->delete();

Toast::title(trans('tomato-notifications::global.notifications.success'))->success()->autoDismiss(2);
return redirect()->back();
}

public function read()
{
$notifications = UserNotification::where('model_type',config('tomato-crm.model'))
->where('model_id', auth('accounts')->user()->id)
->orderBy('id', 'desc')
->take(10)->get();

foreach ($notifications as $notification){
$notification->read();
}

Toast::title(__('Notifications is marked as read'))->success()->autoDismiss(2);
return redirect()->back();
}

public function readSelected(UserNotification $model)
{
if(auth('accounts')->user()->id === $model->model_id){
$model->read();

Toast::title(__('Notifications is marked as read'))->success()->autoDismiss(2);
return redirect()->back();
}
else {
return redirect()->back();
}
}

public function destroy(UserNotification $model)
{
if(auth('accounts')->user()->id === $model->model_id){
$model->delete();

Toast::title(__('Notifications is deleted'))->success()->autoDismiss(2);
return redirect()->back();
}
else {
return redirect()->back();
}
}

public function token(Request $request)
{
$request->validate([
"token" => "required|string",
]);

$checkEx = UserToken::where('model_type', config('tomato-crm.model'))
->where('model_id', auth('accounts')->user()->id)
->where('provider', 'fcm-web')
->first();

if (!$checkEx) {
$token = new UserToken();
$token->model_type = config('tomato-crm.model');
$token->model_id = auth('accounts')->user()->id;
$token->provider = 'fcm-web';
$token->provider_token = $request->get('token');
$token->save();

return back();
} else {
$checkEx->provider_token = $request->get('token');
$checkEx->save();

return back();
}
}

}
6 changes: 6 additions & 0 deletions Modules/CircleXO/App/Models/AccountListing.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@

use App\Models\Account;
use Illuminate\Database\Eloquent\Model;
use Multicaret\Acquaintances\Traits\CanBeLiked;
use Multicaret\Acquaintances\Traits\CanBeRated;
use Multicaret\Acquaintances\Traits\CanLike;
use Multicaret\Acquaintances\Traits\CanRate;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;

class AccountListing extends Model implements HasMedia
{
use InteractsWithMedia;
use CanLike, CanBeLiked;
use CanRate, CanBeRated;

/**
* The attributes that are mass assignable.
Expand Down
14 changes: 10 additions & 4 deletions Modules/CircleXO/resources/views/auth/login.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@seoKeywords(setting('site_keywords'))

<x-circle-xo-app-layout>
<div class="h-screen flex flex-col justify-center items-center mx-6">
<div class="min-h-screen flex flex-col justify-center items-center mx-6 my-8 lg:my-16">
<div class="w-full justify-between flex">
<div class="flex flex-col justify-center items-center w-full">
<x-circle-xo-card
Expand All @@ -28,9 +28,15 @@ class="w-full lg:w-1/2"
icon="bxs-log-in"
>
<x-splade-form method="POST" :action="route('account.login.check')" class="flex flex-col gap-4 w-full">
<x-splade-input type="text" name="username" label="Username" />
<x-splade-input type="password" name="password" label="Password" />
<x-splade-button label="Login" class="bg-main-600 border-main-400 text-gray-900" />
<x-splade-input type="text" name="username" :label="__('Username')" />
<x-splade-input type="password" name="password" :label="__('Password')" />
<x-splade-submit spinner :label="__('Login')" class="bg-main-600 border-main-400 text-gray-900" />
<p class="mt-4 text-sm text-gray-300 sm:mt-0">
{{__("Don't have an account?")}}
<x-splade-link href="{{route('account.register')}}" class="text-gray-400 underline">{{__('Register')}}</x-splade-link>.
{{__("lose your password?")}}
<x-splade-link href="{{route('account.reset')}}" class="text-gray-400 underline">{{__('Reset Password')}}</x-splade-link>.
</p>
</x-splade-form>
</x-circle-xo-card>
</div>
Expand Down
10 changes: 7 additions & 3 deletions Modules/CircleXO/resources/views/auth/otp.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@seoKeywords(setting('site_keywords'))

<x-circle-xo-app-layout>
<div class="h-screen flex flex-col justify-center items-center mx-6">
<div class="min-h-screen flex flex-col justify-center items-center mx-6 my-8 lg:my-16">
<div class="w-full justify-between flex">
<div class="flex flex-col justify-center items-center w-full">
<x-circle-xo-card
Expand All @@ -28,8 +28,12 @@ class="w-full lg:w-1/2"
icon="bxs-check-circle"
>
<x-splade-form method="POST" :action="route('account.otp.check')" class="flex flex-col gap-4 w-full">
<x-splade-input type="number" name="otp_code" label="Code" />
<x-splade-button label="Verify" class="bg-main-600 border-main-400 text-gray-900" />
<x-splade-input type="number" name="otp_code" :label="__('OTP Code')" />
<x-splade-submit spinner :label="__('Verify')" class="bg-main-600 border-main-400 text-gray-900" />
<p class="mt-4 text-sm text-gray-300 sm:mt-0">
{{__("Don't get the code?")}}
<x-splade-link method="POST" href="{{route('account.otp.resend')}}" class="text-gray-400 underline">{{__('Resend Email')}}</x-splade-link>.
</p>
</x-splade-form>
</x-circle-xo-card>
</div>
Expand Down
Loading

0 comments on commit 48e4431

Please sign in to comment.