Browse Source

:art: Allow multiple tokens by email

So that you can have an email with multiple browsers or smartphone
master
Clement Desmidt 4 months ago
parent
commit
8fa74f62e8

+ 11
- 15
app/Http/Controllers/UsersController.php View File

@@ -20,24 +20,20 @@ class UsersController extends Controller
} catch (ValidationException $e) {
return response()->json(['error' => $e->errors()], 422);
}
try {
$data = $this->validate($request, [
'email' => 'unique:users',
]);
} catch (ValidationException $e) {
return response()->json(['error' => $e->errors()], 409);
}
$token = User::generateToken();

$user = new User(['email' => $data['email']]);
$user->api_token = $token;
$user->save();
$user = User::where('email', $data['email'])->first();
if (!$user instanceof User) {
$user = new User(['email' => $data['email']]);
$user->save();
}

$token = $user->addToken();

Mail::to($user->email)->send(new Confirmation($user));
if (!$user->confirmed ) {
Mail::to($user->email)->send(new Confirmation($user));
}

return response()->json(['status' => 'success', 'message' => 'mail sent']);
return response()->json(['status' => 'success', 'mail_sent' => !$user->confirmed, 'message' => 'mail sent', 'token' => $token->api_token]);
}

public function confirm(Request $request)

+ 10
- 0
app/Token.php View File

@@ -0,0 +1,10 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Token extends Model
{
protected $table = 'tokens';
}

+ 31
- 2
app/User.php View File

@@ -15,10 +15,10 @@ class User extends Model implements Authenticatable
*/
protected $table = 'users';

protected $fillable = ["email"];
protected $fillable = ['email'];

public static $rules = [
"email" => "required",
'email' => 'required',
];

/**
@@ -92,4 +92,33 @@ class User extends Model implements Authenticatable
{
return null;
}

/**
* @return Token[]
*/
public function getTokens()
{
return Token::where('user_id', $this->id)->all();
}

/**
* @param null|string $name
*
* @return bool|Token
*/
public function addToken($name = null)
{
try {
$new_token = new Token();
$new_token->user_id = $this->id;
$new_token->api_token = self::generateToken();
$new_token->token_name = $name;

$new_token->save();

return $new_token;
} catch (\Exception $e) {
return $e->getMessage();
}
}
}

+ 42
- 0
database/migrations/2019_04_18_210201_create_tokens.php View File

@@ -0,0 +1,42 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTokens extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tokens', static function (Blueprint $table) {
$table->increments('id');
$table->unsignedBigInteger('user_id');
$table->string('api_token')->unique();
$table->string('token_name')->nullable();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});

Schema::table('users', static function (Blueprint $table) {
$table->dropColumn('api_token');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tokens');
Schema::table('users', static function(Blueprint $table) {
$table->string('api_token')->unique();
});
}
}

+ 10631
- 0
package-lock.json
File diff suppressed because it is too large
View File


+ 5
- 0
package.json View File

@@ -18,5 +18,10 @@
"lodash": "^4.17.5",
"popper.js": "^1.12",
"vue": "^2.5.7"
},
"dependencies": {
"clipboard-copy": "^3.0.0",
"detect-browser": "^4.4.0",
"vue-resource": "^1.5.1"
}
}

+ 17
- 2
resources/js/components/App.vue View File

@@ -11,12 +11,16 @@
<button class="btn btn-primary btn-lg" type="submit" @click="register()">Generate</button>
<div v-show="success" class="alert alert-success" role="alert">{{ message }}</div>
<div v-show="error" class="alert alert-warning" role="alert">{{ message }}</div>
<div class="info" v-show="token !== ''" id="token_clip">{{ token }}</div>
<button class="btn btn-primary btn-lg" v-show="resend" type="submit" @click="resendAction()">Resend link</button>
</div>
</div>
</template>

<script>
const { detect } = require('detect-browser');
const browser = detect();
const copy = require('clipboard-copy');
export default {
name: 'registration',
resource: null,
@@ -28,18 +32,29 @@
error: false,
resend: false,
message: '',
token: ''
}
},
methods: {
register: function() {
this.resetMessages();
this.loading = true;
var formData = new FormData();
let formData = new FormData();
formData.append('email', this.email);
Vue.http.post('api/register', formData, {emulateJSON: false}).then((response) => {
let data = response.data;
this.token = data.token ? data.token : '';
this.loading = false;
this.success = true;
this.message = 'An email has been sent to verify it.';
this.message = '';
if (data.mail_sent) {
this.message = 'An email has been sent to verify it.';
}
document.addEventListener('click',function(e){
if (e.target && e.target.id === "token_clip") {
copy(data.token);
}
});
}, (response) => {
this.loading = false;
this.error = true;

Loading…
Cancel
Save