diff --git a/.env.example b/.env.example index 11c8600..2bc90f8 100644 --- a/.env.example +++ b/.env.example @@ -10,6 +10,10 @@ LOG_LEVEL=debug DB_CONNECTION=sqlite +GITHUB_CLIENT_ID= +GITHUB_CLIENT_SECRET= +GITHUB_CLIENT_CALLBACK=http://localhost:8000/callback/github + BROADCAST_DRIVER=log CACHE_DRIVER=file FILESYSTEM_DISK=local @@ -23,8 +27,11 @@ REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379 -MAIL_MAILER=log -MAIL_FROM_NAME="${APP_NAME}" +MAIL_MAILER=smtp +MAIL_HOST=sandbox.smtp.mailtrap.io +MAIL_PORT=2525 +MAIL_USERNAME= +MAIL_PASSWORD= AWS_ACCESS_KEY_ID= AWS_SECRET_ACCESS_KEY= diff --git a/app/Http/Controllers/BlogController.php b/app/Http/Controllers/BlogController.php index b0ebbd5..f7b4c21 100644 --- a/app/Http/Controllers/BlogController.php +++ b/app/Http/Controllers/BlogController.php @@ -56,7 +56,7 @@ public function create() return view('blog.create'); } - public function insert() + public function store() { request()->validate([ 'title' => 'required', @@ -65,9 +65,7 @@ public function insert() $id = request()->user()->id; $image = BlogController::getRequestImage(); - - Log::info($image); - Log::info("Helllo"); + dd($image); $blog = Blog::create([ 'title' => request('title'), 'containt' => request('containt'), @@ -97,7 +95,7 @@ public function delete($id) if (!$blog) return response('', 404); if (request()->user()->id != $blog->user->id) return response('', 401); $blog->delete(); - return response('', 200); + return redirect('/'); } public function update($id) @@ -112,13 +110,13 @@ public function update($id) if (request()->user()->id != $blog->user->id) return response('', 401); $image = BlogController::getRequestImage(); - + dd($image); $blog->title = request('title'); $blog->containt = request('containt'); $blog->epilog = request('epilog'); if($image) $blog->image = $image; $blog->save(); - return response('', 200); + return redirect('/blog/' . $id); } } diff --git a/app/Http/Controllers/SocialiteController.php b/app/Http/Controllers/SocialiteController.php new file mode 100644 index 0000000..d502b21 --- /dev/null +++ b/app/Http/Controllers/SocialiteController.php @@ -0,0 +1,61 @@ +providers)) + { + return Socialite::driver($provider)->redirect(); + } + return response("", 404); + } + + public function callback($provider) + { + if(in_array($provider, $this->providers)) + { + $user = null; + try { + $user = Socialite::driver($provider)->user(); + } catch(Exception $e) { + return redirect('/register')->withErrors([ + "message" => "Failed to login with " . $provider + ]); + } + $email = $user->getEmail(); + $name = $user->getName(); + $username = $user->getNickname(); + + $user = User::where("email", $email)->first(); + + if(!$user) + { + $user = User::create([ + 'name' => $name, + 'email' => $email, + 'username' => $username, + 'password' => bcrypt(Str::Random(15)), + 'email_verified_at' => Carbon::now() + ]); + } + + Auth::login($user); + if (Auth::check()) return redirect('/'); + } + return response("", 404); + } +} diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 61b3114..8760061 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -42,10 +42,22 @@ public function update($id) $user->username = request('username'); $user->name = request('name'); $user->description = request('description'); + $user->image = UserController::getRequestImage(); $user->save(); return response('', 200); } + private static function getRequestImage() + { + if(request()->hasFile('image')) + { + $hash = request()->file('image')->hashName(); + request()->file('image')->store('public/images/profile'); + return $hash; + } + else return ""; + } + public function follow($id) { $user = User::find($id); diff --git a/app/Models/User.php b/app/Models/User.php index 23cfb7e..f0bbd70 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -27,6 +27,7 @@ class User extends Authenticatable implements MustVerifyEmail 'image', 'email', 'password', + 'email_verified_at' ]; /** diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index e20deb4..2089d8e 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -5,6 +5,7 @@ use Illuminate\Support\ServiceProvider; use Illuminate\Auth\Notifications\VerifyEmail; use Illuminate\Notifications\Messages\MailMessage; +use Laravel\Socialite\Contracts\Factory; class AppServiceProvider extends ServiceProvider { diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 2d65aac..02da8b2 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -6,6 +6,7 @@ use Illuminate\Auth\Listeners\SendEmailVerificationNotification; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; use Illuminate\Support\Facades\Event; +use App\Providers\SocialiteKainooSSOServiceProvider; class EventServiceProvider extends ServiceProvider { diff --git a/app/Providers/SocialiteKainooSSOProvider.php b/app/Providers/SocialiteKainooSSOProvider.php new file mode 100644 index 0000000..3104b69 --- /dev/null +++ b/app/Providers/SocialiteKainooSSOProvider.php @@ -0,0 +1,58 @@ +buildAuthUrlFromBase($this->getSiteUrl() . '/auth', $state); + } + + protected function getTokenUrl() + { + return $this->getSiteUrl() . '/token'; + } + + protected function getUserByToken($token) + { + $response = $this->getHttpClient()->post($this->getSiteUrl() . '/userinfo', [ + 'headers' => [ + 'cache-control' => 'no-cache', + 'Authorization' => 'Bearer ' . $token, + 'Content-Type' => 'application/x-www-form-urlencoded', + ] + ]); + + return json_decode($response->getBody()->getContents(), true); + } + + protected function mapUserToObject(array $user) + { + return (new SocialiteUser())->setRaw($user)->map([ + 'id' => $user['sub'], + 'name' => $user['name'], + 'email' => $user['email'], + 'nickname' => $user['given_name'], + 'name' => $user['family_name'], + ]); + } +} diff --git a/app/Providers/SocialiteKainooSSOServiceProvider.php b/app/Providers/SocialiteKainooSSOServiceProvider.php new file mode 100644 index 0000000..9f343c0 --- /dev/null +++ b/app/Providers/SocialiteKainooSSOServiceProvider.php @@ -0,0 +1,21 @@ +app->make('Laravel\Socialite\Contracts\Factory'); + $socialite->extend( + 'kainoo-sso', + function ($app) use ($socialite) { + $config = config('services.kainoo-sso'); + return $socialite->buildProvider(SocialiteKainooSSOProvider::class, $config); + } + ); + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index c1d7692..5939fcd 100644 --- a/composer.json +++ b/composer.json @@ -9,6 +9,7 @@ "guzzlehttp/guzzle": "^7.2", "laravel/framework": "^10.10", "laravel/sanctum": "^3.3", + "laravel/socialite": "^5.15", "laravel/tinker": "^2.8", "nyholm/psr7": "^1.8", "railsware/mailtrap-php": "^2.0", diff --git a/composer.lock b/composer.lock index 05779af..e1132a4 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0d06ab3ef1d3439c90037cd4c2c3a39e", + "content-hash": "7224b1e6a744d5216e58d67ddddb21d7", "packages": [ { "name": "brick/math", @@ -572,6 +572,69 @@ ], "time": "2023-10-06T06:47:41+00:00" }, + { + "name": "firebase/php-jwt", + "version": "v6.10.1", + "source": { + "type": "git", + "url": "https://github.com/firebase/php-jwt.git", + "reference": "500501c2ce893c824c801da135d02661199f60c5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/firebase/php-jwt/zipball/500501c2ce893c824c801da135d02661199f60c5", + "reference": "500501c2ce893c824c801da135d02661199f60c5", + "shasum": "" + }, + "require": { + "php": "^8.0" + }, + "require-dev": { + "guzzlehttp/guzzle": "^7.4", + "phpspec/prophecy-phpunit": "^2.0", + "phpunit/phpunit": "^9.5", + "psr/cache": "^2.0||^3.0", + "psr/http-client": "^1.0", + "psr/http-factory": "^1.0" + }, + "suggest": { + "ext-sodium": "Support EdDSA (Ed25519) signatures", + "paragonie/sodium_compat": "Support EdDSA (Ed25519) signatures when libsodium is not present" + }, + "type": "library", + "autoload": { + "psr-4": { + "Firebase\\JWT\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Neuman Vong", + "email": "neuman+pear@twilio.com", + "role": "Developer" + }, + { + "name": "Anant Narayanan", + "email": "anant@php.net", + "role": "Developer" + } + ], + "description": "A simple library to encode and decode JSON Web Tokens (JWT) in PHP. Should conform to the current spec.", + "homepage": "https://github.com/firebase/php-jwt", + "keywords": [ + "jwt", + "php" + ], + "support": { + "issues": "https://github.com/firebase/php-jwt/issues", + "source": "https://github.com/firebase/php-jwt/tree/v6.10.1" + }, + "time": "2024-05-18T18:05:11+00:00" + }, { "name": "fruitcake/php-cors", "version": "v1.3.0", @@ -1508,6 +1571,78 @@ }, "time": "2024-08-02T07:48:17+00:00" }, + { + "name": "laravel/socialite", + "version": "v5.15.1", + "source": { + "type": "git", + "url": "https://github.com/laravel/socialite.git", + "reference": "cc02625f0bd1f95dc3688eb041cce0f1e709d029" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/laravel/socialite/zipball/cc02625f0bd1f95dc3688eb041cce0f1e709d029", + "reference": "cc02625f0bd1f95dc3688eb041cce0f1e709d029", + "shasum": "" + }, + "require": { + "ext-json": "*", + "firebase/php-jwt": "^6.4", + "guzzlehttp/guzzle": "^6.0|^7.0", + "illuminate/contracts": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", + "illuminate/http": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", + "illuminate/support": "^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", + "league/oauth1-client": "^1.10.1", + "php": "^7.2|^8.0", + "phpseclib/phpseclib": "^3.0" + }, + "require-dev": { + "mockery/mockery": "^1.0", + "orchestra/testbench": "^4.0|^5.0|^6.0|^7.0|^8.0|^9.0", + "phpstan/phpstan": "^1.10", + "phpunit/phpunit": "^8.0|^9.3|^10.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "5.x-dev" + }, + "laravel": { + "providers": [ + "Laravel\\Socialite\\SocialiteServiceProvider" + ], + "aliases": { + "Socialite": "Laravel\\Socialite\\Facades\\Socialite" + } + } + }, + "autoload": { + "psr-4": { + "Laravel\\Socialite\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "Laravel wrapper around OAuth 1 & OAuth 2 libraries.", + "homepage": "https://laravel.com", + "keywords": [ + "laravel", + "oauth" + ], + "support": { + "issues": "https://github.com/laravel/socialite/issues", + "source": "https://github.com/laravel/socialite" + }, + "time": "2024-06-28T20:09:34+00:00" + }, { "name": "laravel/tinker", "version": "v2.9.0", @@ -1950,6 +2085,82 @@ ], "time": "2024-01-28T23:22:08+00:00" }, + { + "name": "league/oauth1-client", + "version": "v1.10.1", + "source": { + "type": "git", + "url": "https://github.com/thephpleague/oauth1-client.git", + "reference": "d6365b901b5c287dd41f143033315e2f777e1167" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/thephpleague/oauth1-client/zipball/d6365b901b5c287dd41f143033315e2f777e1167", + "reference": "d6365b901b5c287dd41f143033315e2f777e1167", + "shasum": "" + }, + "require": { + "ext-json": "*", + "ext-openssl": "*", + "guzzlehttp/guzzle": "^6.0|^7.0", + "guzzlehttp/psr7": "^1.7|^2.0", + "php": ">=7.1||>=8.0" + }, + "require-dev": { + "ext-simplexml": "*", + "friendsofphp/php-cs-fixer": "^2.17", + "mockery/mockery": "^1.3.3", + "phpstan/phpstan": "^0.12.42", + "phpunit/phpunit": "^7.5||9.5" + }, + "suggest": { + "ext-simplexml": "For decoding XML-based responses." + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev", + "dev-develop": "2.0-dev" + } + }, + "autoload": { + "psr-4": { + "League\\OAuth1\\Client\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ben Corlett", + "email": "bencorlett@me.com", + "homepage": "http://www.webcomm.com.au", + "role": "Developer" + } + ], + "description": "OAuth 1.0 Client Library", + "keywords": [ + "Authentication", + "SSO", + "authorization", + "bitbucket", + "identity", + "idp", + "oauth", + "oauth1", + "single sign on", + "trello", + "tumblr", + "twitter" + ], + "support": { + "issues": "https://github.com/thephpleague/oauth1-client/issues", + "source": "https://github.com/thephpleague/oauth1-client/tree/v1.10.1" + }, + "time": "2022-04-15T14:02:14+00:00" + }, { "name": "monolog/monolog", "version": "3.7.0", @@ -2528,6 +2739,123 @@ ], "time": "2023-11-13T09:31:12+00:00" }, + { + "name": "paragonie/constant_time_encoding", + "version": "v3.0.0", + "source": { + "type": "git", + "url": "https://github.com/paragonie/constant_time_encoding.git", + "reference": "df1e7fde177501eee2037dd159cf04f5f301a512" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/paragonie/constant_time_encoding/zipball/df1e7fde177501eee2037dd159cf04f5f301a512", + "reference": "df1e7fde177501eee2037dd159cf04f5f301a512", + "shasum": "" + }, + "require": { + "php": "^8" + }, + "require-dev": { + "phpunit/phpunit": "^9", + "vimeo/psalm": "^4|^5" + }, + "type": "library", + "autoload": { + "psr-4": { + "ParagonIE\\ConstantTime\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Paragon Initiative Enterprises", + "email": "security@paragonie.com", + "homepage": "https://paragonie.com", + "role": "Maintainer" + }, + { + "name": "Steve 'Sc00bz' Thomas", + "email": "steve@tobtu.com", + "homepage": "https://www.tobtu.com", + "role": "Original Developer" + } + ], + "description": "Constant-time Implementations of RFC 4648 Encoding (Base-64, Base-32, Base-16)", + "keywords": [ + "base16", + "base32", + "base32_decode", + "base32_encode", + "base64", + "base64_decode", + "base64_encode", + "bin2hex", + "encoding", + "hex", + "hex2bin", + "rfc4648" + ], + "support": { + "email": "info@paragonie.com", + "issues": "https://github.com/paragonie/constant_time_encoding/issues", + "source": "https://github.com/paragonie/constant_time_encoding" + }, + "time": "2024-05-08T12:36:18+00:00" + }, + { + "name": "paragonie/random_compat", + "version": "v9.99.100", + "source": { + "type": "git", + "url": "https://github.com/paragonie/random_compat.git", + "reference": "996434e5492cb4c3edcb9168db6fbb1359ef965a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/paragonie/random_compat/zipball/996434e5492cb4c3edcb9168db6fbb1359ef965a", + "reference": "996434e5492cb4c3edcb9168db6fbb1359ef965a", + "shasum": "" + }, + "require": { + "php": ">= 7" + }, + "require-dev": { + "phpunit/phpunit": "4.*|5.*", + "vimeo/psalm": "^1" + }, + "suggest": { + "ext-libsodium": "Provides a modern crypto API that can be used to generate random bytes." + }, + "type": "library", + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Paragon Initiative Enterprises", + "email": "security@paragonie.com", + "homepage": "https://paragonie.com" + } + ], + "description": "PHP 5.x polyfill for random_bytes() and random_int() from PHP 7", + "keywords": [ + "csprng", + "polyfill", + "pseudorandom", + "random" + ], + "support": { + "email": "info@paragonie.com", + "issues": "https://github.com/paragonie/random_compat/issues", + "source": "https://github.com/paragonie/random_compat" + }, + "time": "2020-10-15T08:29:30+00:00" + }, { "name": "php-http/client-common", "version": "2.7.1", @@ -2984,6 +3312,116 @@ ], "time": "2024-07-20T21:41:07+00:00" }, + { + "name": "phpseclib/phpseclib", + "version": "3.0.41", + "source": { + "type": "git", + "url": "https://github.com/phpseclib/phpseclib.git", + "reference": "621c73f7dcb310b61de34d1da4c4204e8ace6ceb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpseclib/phpseclib/zipball/621c73f7dcb310b61de34d1da4c4204e8ace6ceb", + "reference": "621c73f7dcb310b61de34d1da4c4204e8ace6ceb", + "shasum": "" + }, + "require": { + "paragonie/constant_time_encoding": "^1|^2|^3", + "paragonie/random_compat": "^1.4|^2.0|^9.99.99", + "php": ">=5.6.1" + }, + "require-dev": { + "phpunit/phpunit": "*" + }, + "suggest": { + "ext-dom": "Install the DOM extension to load XML formatted public keys.", + "ext-gmp": "Install the GMP (GNU Multiple Precision) extension in order to speed up arbitrary precision integer arithmetic operations.", + "ext-libsodium": "SSH2/SFTP can make use of some algorithms provided by the libsodium-php extension.", + "ext-mcrypt": "Install the Mcrypt extension in order to speed up a few other cryptographic operations.", + "ext-openssl": "Install the OpenSSL extension in order to speed up a wide variety of cryptographic operations." + }, + "type": "library", + "autoload": { + "files": [ + "phpseclib/bootstrap.php" + ], + "psr-4": { + "phpseclib3\\": "phpseclib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jim Wigginton", + "email": "terrafrost@php.net", + "role": "Lead Developer" + }, + { + "name": "Patrick Monnerat", + "email": "pm@datasphere.ch", + "role": "Developer" + }, + { + "name": "Andreas Fischer", + "email": "bantu@phpbb.com", + "role": "Developer" + }, + { + "name": "Hans-Jürgen Petrich", + "email": "petrich@tronic-media.com", + "role": "Developer" + }, + { + "name": "Graham Campbell", + "email": "graham@alt-three.com", + "role": "Developer" + } + ], + "description": "PHP Secure Communications Library - Pure-PHP implementations of RSA, AES, SSH2, SFTP, X.509 etc.", + "homepage": "http://phpseclib.sourceforge.net", + "keywords": [ + "BigInteger", + "aes", + "asn.1", + "asn1", + "blowfish", + "crypto", + "cryptography", + "encryption", + "rsa", + "security", + "sftp", + "signature", + "signing", + "ssh", + "twofish", + "x.509", + "x509" + ], + "support": { + "issues": "https://github.com/phpseclib/phpseclib/issues", + "source": "https://github.com/phpseclib/phpseclib/tree/3.0.41" + }, + "funding": [ + { + "url": "https://github.com/terrafrost", + "type": "github" + }, + { + "url": "https://www.patreon.com/phpseclib", + "type": "patreon" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpseclib/phpseclib", + "type": "tidelift" + } + ], + "time": "2024-08-12T00:13:54+00:00" + }, { "name": "psr/clock", "version": "1.0.0", diff --git a/config/app.php b/config/app.php index 9207160..f1f4c8b 100644 --- a/config/app.php +++ b/config/app.php @@ -167,7 +167,9 @@ App\Providers\AuthServiceProvider::class, // App\Providers\BroadcastServiceProvider::class, App\Providers\EventServiceProvider::class, + Laravel\Socialite\SocialiteServiceProvider::class, App\Providers\RouteServiceProvider::class, + App\Providers\SocialiteKainooSSOServiceProvider::class ])->toArray(), /* diff --git a/config/services.php b/config/services.php index 0ace530..4c042e4 100644 --- a/config/services.php +++ b/config/services.php @@ -13,6 +13,17 @@ | a conventional file to locate the various service credentials. | */ + 'kainoo-sso' => [ + 'client_id' => env('KAINOO_SSO_CLIENT_ID'), + 'client_secret' => env('KAINOO_SSO_CLIENT_SECRET'), + 'redirect' => env('KAINOO_SSO_CLIENT_CALLBACK') + ], + + 'github' => [ + 'client_id' => env('GITHUB_CLIENT_ID'), + 'client_secret' => env('GITHUB_CLIENT_SECRET'), + 'redirect' => env('GITHUB_CLIENT_CALLBACK') + ], 'mailgun' => [ 'domain' => env('MAILGUN_DOMAIN'), diff --git a/resources/css/app.css b/resources/css/app.css index add07ff..f8ffd5a 100644 --- a/resources/css/app.css +++ b/resources/css/app.css @@ -15,6 +15,10 @@ @layer utilities { .modal::backdrop { background-color: rgba(0, 0, 0, 0.63); } + + .rond-img { + clip-path:circle(40%); + } } :root { @@ -26,7 +30,7 @@ body { } .md-content { - font-family: "Courier Prime"; + /*font-family: "Courier Prime";*/ } .md-content h1 { diff --git a/resources/js/app.js b/resources/js/app.js new file mode 100644 index 0000000..9a353ac --- /dev/null +++ b/resources/js/app.js @@ -0,0 +1,8 @@ +import { darkMode } from "./commun" + + +if (localStorage.getItem("darkMode") && localStorage.getItem("darkMode") === "yes") { + darkMode(true); +} else { + darkMode(false); +} \ No newline at end of file diff --git a/resources/js/blog.js b/resources/js/blog.js index 8b3fc57..6352b3d 100644 --- a/resources/js/blog.js +++ b/resources/js/blog.js @@ -1,8 +1,5 @@ import './bootstrap'; - -function addEvent(btn, func) { - if(btn) func(btn); -} +import { addEvent } from "./commun" window.addEventListener("DOMContentLoaded", () => { let modal = document.querySelector('#modal-update'); @@ -22,11 +19,13 @@ window.addEventListener("DOMContentLoaded", () => { method: "POST", headers: { "X-CSRF-Token": token + }, + }).then(response => { + if (response.redirected) { + window.location.href = response.url; + return; } - }).then((res) => { - if (res.status === 400) alert("Couldn't follow this user"); - //else window.location.href = "/profile/" + id; - }); + }); }); }); @@ -39,11 +38,12 @@ window.addEventListener("DOMContentLoaded", () => { method: "POST", headers: { "X-CSRF-Token": token + }, + }).then(response => { + if (response.redirected) { + window.location.href = response.url; } - }).then((res) => { - if (res.status === 400) alert(res.value); - else window.location.href = "/blog/" + id; - }); + }); }); }); @@ -71,8 +71,12 @@ window.addEventListener("DOMContentLoaded", () => { headers: { "X-CSRF-Token": token }, - body: formData - }); + body: formData, + }).then(response => { + if (response.redirected) { + window.location.href = response.url; + } + }); }); }); @@ -85,10 +89,12 @@ window.addEventListener("DOMContentLoaded", () => { method: "DELETE", headers: { "X-CSRF-Token": token + }, + }).then(response => { + if (response.redirected) { + window.location.href = response.url; } - }).then((res) => { - window.location.href = "/blogs" - }); + }); }); }); }) \ No newline at end of file diff --git a/resources/js/commun.js b/resources/js/commun.js new file mode 100644 index 0000000..7d2b33e --- /dev/null +++ b/resources/js/commun.js @@ -0,0 +1,14 @@ +export function addEvent(btn, func) { + if(btn) func(btn); +} + +export function darkMode(state) { + let page = document.querySelector("html"); + if (state && !page.classList.contains("dark")) { + page.classList.add("dark"); + localStorage.setItem("darkMode", "yes"); + } else { + localStorage.setItem("darkMode", "no"); + page.classList.remove("dark"); + } +} \ No newline at end of file diff --git a/resources/js/user.js b/resources/js/user.js index abac59c..7cc0821 100644 --- a/resources/js/user.js +++ b/resources/js/user.js @@ -1,8 +1,5 @@ import './bootstrap'; - -function addEvent(btn, func) { - if(btn) func(btn); -} +import { darkMode, addEvent } from "./commun" window.addEventListener("DOMContentLoaded", () => { let modal = document.querySelector('#modal-update'); @@ -11,6 +8,22 @@ window.addEventListener("DOMContentLoaded", () => { let deleteUserBtn = document.querySelector("#btn-user-delete"); let updateUserBtn = document.querySelector("#btn-user-update"); let followBtn = document.querySelector("#btn-follow"); + let darkModeToggle = document.querySelector("#dark-mode-toggle"); + + if (localStorage.getItem("darkMode") && localStorage.getItem("darkMode") === "yes") { + darkModeToggle.checked = true + } else { + darkModeToggle.checked = false + } + + addEvent(darkModeToggle, (btn) => btn.addEventListener("change", function() { + console.log("coucou") + if (this.checked){ + darkMode(true); + } else { + darkMode(false); + } + })); addEvent(followBtn, (btn) => { btn.addEventListener("click", (e) => { diff --git a/resources/views/auth/login.blade.php b/resources/views/auth/login.blade.php index d833913..dfbfb72 100644 --- a/resources/views/auth/login.blade.php +++ b/resources/views/auth/login.blade.php @@ -6,9 +6,9 @@