|
| 1 | +## Laravel & Nuxt CLI Authentication - Laravel Part |
| 2 | + |
| 3 | +A. Install & setup Sanctum |
| 4 | + |
| 5 | + 1) composer require laravel/sanctum |
| 6 | + 2 ) php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider" |
| 7 | + 3 ) php artisan migrate |
| 8 | + 4 ) EnsureFrontendRequestsAreStateful::class in Karnel.php |
| 9 | + |
| 10 | +B. .env |
| 11 | + **Laravel Domain** |
| 12 | + SESSION_DOMAIN=localhost |
| 13 | + |
| 14 | + **Nuxt Domain** |
| 15 | + SANCTUM_STATEFUL_DOMAINS=localhost:3000 |
| 16 | + |
| 17 | +C. routes/api.php |
| 18 | + Route::middleware('auth:sanctum')->get('/user', function (Request $request) { |
| 19 | + return $request->user(); |
| 20 | + }); |
| 21 | + // Login & Logout routes |
| 22 | + Route::post('/login', 'Auth\LoginController@login'); |
| 23 | + Route::post('/logout', 'Auth\LoginController@logout'); |
| 24 | + |
| 25 | +D. config/cors.php - replace |
| 26 | + 'paths' => ['api/*', 'sanctum/csrf-cookie'], |
| 27 | + into |
| 28 | + 'paths' => ['api/*'], |
| 29 | + |
| 30 | +E. App/Users.php |
| 31 | + use HasApiTokens |
| 32 | + |
| 33 | +F. App/Http/Controllers/Auth/LoginController.php |
| 34 | + public function login(Request $request) |
| 35 | + { |
| 36 | + $request->validate([ |
| 37 | + 'email' => 'required', |
| 38 | + 'password' => 'required' |
| 39 | + ]); |
| 40 | + if (!Auth::attempt($request->only('email', 'password'))) { |
| 41 | + throw new AuthenticationException(); |
| 42 | + } |
| 43 | + return $this->jsonResponse(true, auth()->user(), 200); |
| 44 | + } |
| 45 | + |
| 46 | + public function logout() |
| 47 | + { |
| 48 | + if (auth()->check()) { |
| 49 | + Auth::logout(); |
| 50 | + return $this->jsonResponse(true, 'Logged out successfully', 200); |
| 51 | + } |
| 52 | + return $this->jsonResponse(true, 'Something went wrong', 404); |
| 53 | + } |
| 54 | + |
| 55 | + private function jsonResponse($success, $data, $status) |
| 56 | + { |
| 57 | + return response()->json([ |
| 58 | + 'success' => $success, |
| 59 | + 'data' => $data, |
| 60 | + ], $status); |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | +## Nuxt / Vue cli |
| 65 | +A. login.vue |
| 66 | + 1 ) import axios from 'axios' |
| 67 | + 2 ) axios.defaults.withCredentials = true |
| 68 | + 3 ) make an helper function - laravelCsrfCookies() |
| 69 | + every time before any function call this function and return true . |
| 70 | + |
| 71 | + methods: { |
| 72 | + laravelCsrfCoockie() { |
| 73 | + axios.get(this.baseURL + 'sanctum/csrf-cookie').then((response) => { |
| 74 | + return true |
| 75 | + }) |
| 76 | + }, |
| 77 | + login() { |
| 78 | + this.laravelCsrfCoockie() |
| 79 | + axios |
| 80 | + .post(this.baseURL + 'api/login', { |
| 81 | + email: this.email, |
| 82 | + password: this.password, |
| 83 | + }) |
| 84 | + .then((res) => { |
| 85 | + localStorage.setItem('me', JSON.stringify(res.data)) |
| 86 | + this.loggedIn = localStorage.getItem('me') |
| 87 | + this.userName = JSON.parse(localStorage.getItem('me')).data.name |
| 88 | + console.log(this.loggedIn) |
| 89 | + }) |
| 90 | + .catch((err) => { |
| 91 | + console.log(err) |
| 92 | + }) |
| 93 | + }, |
| 94 | + logout() { |
| 95 | + this.laravelCsrfCoockie() |
| 96 | + axios |
| 97 | + .post(this.baseURL + 'api/logout') |
| 98 | + .then((res) => { |
| 99 | + localStorage.removeItem('me') |
| 100 | + this.loggedIn = null |
| 101 | + this.userName = 'Guest User' |
| 102 | + console.log(this.loggedIn) |
| 103 | + }) |
| 104 | + .catch((err) => { |
| 105 | + console.log(err) |
| 106 | + }) |
| 107 | + }, |
| 108 | + |
| 109 | + |
| 110 | +B. Demo Login.vue |
| 111 | + <template> |
| 112 | + <div> |
| 113 | + <button v-if="!loggedIn" @click="login">Login</button> |
| 114 | + <button v-else @click="logout">logout</button> |
| 115 | + <h1 v-if="userName">{{ userName }}</h1> |
| 116 | + </div> |
| 117 | + </template> |
| 118 | + <script> |
| 119 | + import axios from 'axios' |
| 120 | + axios.defaults.withCredentials = true |
| 121 | + |
| 122 | + export default { |
| 123 | + data() { |
| 124 | + return { |
| 125 | + |
| 126 | + password: 'password', |
| 127 | + baseURL: 'http://localhost:8000/', |
| 128 | + loggedIn: localStorage.getItem('me'), |
| 129 | + userName: localStorage.getItem('me') |
| 130 | + ? JSON.parse(localStorage.getItem('me')).data.name |
| 131 | + : 'Guest User', |
| 132 | + } |
| 133 | + }, |
| 134 | + mounted() { |
| 135 | + console.log(this.loggedIn) |
| 136 | + }, |
| 137 | + methods: { |
| 138 | + laravelCsrfCoockie() { |
| 139 | + axios.get(this.baseURL + 'sanctum/csrf-cookie').then((response) => { |
| 140 | + return true |
| 141 | + }) |
| 142 | + }, |
| 143 | + login() { |
| 144 | + this.laravelCsrfCoockie() |
| 145 | + axios |
| 146 | + .post(this.baseURL + 'api/login', { |
| 147 | + email: this.email, |
| 148 | + password: this.password, |
| 149 | + }) |
| 150 | + .then((res) => { |
| 151 | + localStorage.setItem('me', JSON.stringify(res.data)) |
| 152 | + this.loggedIn = localStorage.getItem('me') |
| 153 | + this.userName = JSON.parse(localStorage.getItem('me')).data.name |
| 154 | + console.log(this.loggedIn) |
| 155 | + }) |
| 156 | + .catch((err) => { |
| 157 | + console.log(err) |
| 158 | + }) |
| 159 | + }, |
| 160 | + logout() { |
| 161 | + this.laravelCsrfCoockie() |
| 162 | + axios |
| 163 | + .post(this.baseURL + 'api/logout') |
| 164 | + .then((res) => { |
| 165 | + localStorage.removeItem('me') |
| 166 | + this.loggedIn = null |
| 167 | + this.userName = 'Guest User' |
| 168 | + console.log(this.loggedIn) |
| 169 | + }) |
| 170 | + .catch((err) => { |
| 171 | + console.log(err) |
| 172 | + }) |
| 173 | + }, |
| 174 | + }, |
| 175 | + } |
| 176 | + </script> |
| 177 | + |
| 178 | +**You can use this starter project for to remove the hasslement of Laravel & Sanctum/Vue Cli** |
0 commit comments