Skip to content

Commit 8491efd

Browse files
committed
erro envio email boas vindas
1 parent e3da862 commit 8491efd

File tree

17 files changed

+260
-15
lines changed

17 files changed

+260
-15
lines changed

api/app/Events/UserRegistered.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\Models\User;
6+
use Illuminate\Broadcasting\Channel;
7+
use Illuminate\Broadcasting\InteractsWithSockets;
8+
use Illuminate\Broadcasting\PresenceChannel;
9+
use Illuminate\Broadcasting\PrivateChannel;
10+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
11+
use Illuminate\Foundation\Events\Dispatchable;
12+
use Illuminate\Queue\SerializesModels;
13+
14+
class UserRegistered
15+
{
16+
use Dispatchable, InteractsWithSockets, SerializesModels;
17+
18+
/**
19+
* Create a new event instance.
20+
*/
21+
public function __construct(public User $user)
22+
{
23+
//
24+
25+
}
26+
27+
/**
28+
* Get the channels the event should broadcast on.
29+
*
30+
* @return array<int, \Illuminate\Broadcasting\Channel>
31+
*/
32+
public function broadcastOn(): array
33+
{
34+
return [
35+
new PrivateChannel('channel-name'),
36+
];
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Auth;
4+
5+
use App\Events\UserRegistered;
6+
use App\Http\Controllers\Controller;
7+
use App\Http\Requests\Auth\RegisterRequest;
8+
use App\Http\Resources\User\UserResource;
9+
use App\Models\User;
10+
use Illuminate\Http\Request;
11+
12+
class RegisterController extends Controller
13+
{
14+
//
15+
public function __invoke(RegisterRequest $request) {
16+
17+
$input = $request->validated();
18+
19+
$input['password'] = bcrypt($input['password']);
20+
21+
$user = User::query()->create($input);
22+
23+
UserRegistered::dispatch($user); // Para disparar um evento
24+
25+
return new UserResource($user);
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Auth;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class RegisterRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*/
12+
public function authorize(): bool
13+
{
14+
return true;
15+
}
16+
17+
/**
18+
* Get the validation rules that apply to the request.
19+
*
20+
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
21+
*/
22+
public function rules(): array
23+
{
24+
return [
25+
//
26+
'first_name' => 'string|required',
27+
'last_name' => 'string|required',
28+
'email' => 'email|unique:users|required',
29+
'password' => 'string|required',
30+
];
31+
}
32+
}

api/app/Http/Resources/User/UserResource.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ class UserResource extends JsonResource
1515
public function toArray(Request $request): array
1616
{
1717
return [
18-
'id' => $this->id,
19-
'name' => $this->name,
20-
'email' => $this->email,
18+
'id' => $this->id,
19+
'first_name' => $this->first_name,
20+
'last_name' => $this->last_name,
21+
'email' => $this->email,
2122
];
2223
}
2324
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
use App\Mail\WelcomeMail;
6+
use Illuminate\Contracts\Queue\ShouldQueue;
7+
use Illuminate\Queue\InteractsWithQueue;
8+
use Illuminate\Support\Facades\Mail;
9+
10+
class SendWelcomeEmailListener
11+
{
12+
/**
13+
* Create the event listener.
14+
*/
15+
public function __construct()
16+
{
17+
//
18+
dd('event listener');
19+
}
20+
21+
/**
22+
* Handle the event.
23+
*/
24+
public function handle(object $event): void
25+
{
26+
//
27+
$user = $event->user;
28+
Mail::to($user->email)->send(new WelcomeMail());
29+
}
30+
}

api/app/Mail/WelcomeMail.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace App\Mail;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Contracts\Queue\ShouldQueue;
7+
use Illuminate\Mail\Mailable;
8+
use Illuminate\Mail\Mailables\Content;
9+
use Illuminate\Mail\Mailables\Envelope;
10+
use Illuminate\Queue\SerializesModels;
11+
12+
class WelcomeMail extends Mailable
13+
{
14+
use Queueable, SerializesModels;
15+
16+
/**
17+
* Create a new message instance.
18+
*/
19+
public function __construct()
20+
{
21+
//
22+
}
23+
24+
/**
25+
* Get the message envelope.
26+
*/
27+
public function envelope(): Envelope
28+
{
29+
return new Envelope(
30+
subject: 'Seja vem-vindo ao AgendaMe',
31+
);
32+
}
33+
34+
/**
35+
* Get the message content definition.
36+
*/
37+
public function content(): Content
38+
{
39+
return new Content(
40+
view: 'emails.welcome',
41+
);
42+
}
43+
44+
/**
45+
* Get the attachments for the message.
46+
*
47+
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
48+
*/
49+
public function attachments(): array
50+
{
51+
return [];
52+
}
53+
}

api/app/Models/User.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,17 @@ class User extends Authenticatable
1717
*
1818
* @var array<int, string>
1919
*/
20+
21+
/*ESSE MÉTODO PROTEGE CAMPO POR CAMPO NA HORA DE ARMAZENAR NO BD
2022
protected $fillable = [
21-
'name',
23+
'first_name',
24+
'last_name',
2225
'email',
2326
'password',
24-
];
27+
];*/
28+
29+
//ESSE MÉTODO PROTEGE APENAS O ID NA HORA DE ARMAZENAR NO BD
30+
protected $guarded = ['id'];
2531

2632
/**
2733
* The attributes that should be hidden for serialization.

api/app/Providers/EventServiceProvider.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace App\Providers;
44

5-
use Illuminate\Auth\Events\Registered;
6-
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
5+
use App\Events\UserRegistered;
6+
use App\Listeners\SendWelcomeEmailListener;
77
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
8-
use Illuminate\Support\Facades\Event;
8+
99

1010
class EventServiceProvider extends ServiceProvider
1111
{
@@ -15,9 +15,9 @@ class EventServiceProvider extends ServiceProvider
1515
* @var array<class-string, array<int, class-string>>
1616
*/
1717
protected $listen = [
18-
Registered::class => [
19-
SendEmailVerificationNotification::class,
20-
],
18+
UserRegistered::class => [
19+
SendWelcomeEmailListener::class,
20+
]
2121
];
2222

2323
/**

api/database/factories/UserFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class UserFactory extends Factory
1818
public function definition(): array
1919
{
2020
return [
21-
'name' => fake()->name(),
21+
'first_name' => fake()->firstName(),
22+
'last_name' => fake()->lastName(),
2223
'email' => fake()->unique()->safeEmail(),
2324
'email_verified_at' => now(),
2425
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password

api/database/migrations/2014_10_12_000000_create_users_table.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public function up(): void
1313
{
1414
Schema::create('users', function (Blueprint $table) {
1515
$table->id();
16-
$table->string('name');
16+
$table->string('first_name');
17+
$table->string('last_name');
1718
$table->string('email')->unique();
1819
$table->timestamp('email_verified_at')->nullable();
1920
$table->string('password');
@@ -30,3 +31,4 @@ public function down(): void
3031
Schema::dropIfExists('users');
3132
}
3233
};
34+

api/database/seeders/DatabaseSeeder.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public function run(): void
1515
\App\Models\User::factory(10)->create();
1616

1717
\App\Models\User::factory()->create([
18-
'name' => 'Test User',
18+
'first_name' => 'Test',
19+
'last_name' => 'User',
1920
'email' => 'test@example.com',
2021

2122
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p>Olá,</p>
2+
<p>Seja bem vindo ao AgendaMe</p>
3+

api/routes/api.php

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use App\Http\Controllers\Auth\LoginController;
44
use App\Http\Controllers\Auth\LogoutController;
5+
use App\Http\Controllers\Auth\RegisterController;
56
use App\Http\Controllers\Me\MeController;
67
use Illuminate\Http\Request;
78
use Illuminate\Support\Facades\Route;
@@ -10,3 +11,4 @@
1011

1112
Route::post('login', LoginController::class);
1213
Route::post('logout', LogoutController::class);
14+
Route::post('register', RegisterController::class);
File renamed without changes.

app/src/router/index.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useAuth } from '@/store/auth';
66
const routes = [
77
{
88
path: '/login',
9-
component: () => import('@/layouts/Login.vue'),
9+
component: () => import('@/layouts/Auth.vue'),
1010
beforeEnter: redirectIfAuthenticated,
1111

1212
children: [{
@@ -15,6 +15,17 @@ const routes = [
1515
component: () => import('@/views/Login.vue')
1616
}],
1717
},
18+
{
19+
path: '/cadastrar',
20+
component: () => import('@/layouts/Auth.vue'),
21+
beforeEnter: redirectIfAuthenticated,
22+
23+
children: [{
24+
path: '',
25+
name: 'cadastrar',
26+
component: () => import('@/views/Register.vue')
27+
}],
28+
},
1829

1930
{
2031
path: '/',

app/src/store/auth.js

+9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ export const useAuth = defineStore('auth', {
2323
return axios.post('api/logout').then(() => {
2424
meStore.user = null
2525
})
26+
},
27+
register(firstName, lastName = '', email, password) {
28+
return axios.post('api/register', {
29+
first_name: firstName,
30+
last_name: lastName,
31+
email: email,
32+
password: password
33+
})
34+
2635
}
2736
},
2837
getters: {

app/src/views/Register.vue

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<template>
2+
3+
<VContainer>
4+
<v-text-field label="Nome" v-model="firtName" />
5+
<VTextField label="Sobrenome" v-model="lastName" />
6+
<VTextField label="email" type="email" v-model="email" />
7+
<VTextField label="senha" type="password" v-model="password" />
8+
<v-btn @click="register">Cadastrar</v-btn>
9+
</VContainer>
10+
11+
</template>
12+
13+
<script setup>
14+
import { ref } from 'vue';
15+
import { useAuth } from '@/store/auth';
16+
17+
const firtName = ref('');
18+
const lastName = ref('');
19+
const email = ref('');
20+
const password = ref('');
21+
22+
function register() {
23+
const authStore = useAuth();
24+
authStore.register(firtName.value, lastName.value, email.value, password.value);
25+
26+
}
27+
28+
</script>
29+

0 commit comments

Comments
 (0)