Skip to content
This repository has been archived by the owner on Jun 16, 2021. It is now read-only.

Commit

Permalink
CRY-87 closed #92 added API token generation upon user registration
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaishiyoku committed Dec 29, 2018
1 parent 3bec859 commit a25d58b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
12 changes: 11 additions & 1 deletion app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Ramsey\Uuid\Uuid;

class RegisterController extends Controller
{
Expand Down Expand Up @@ -94,7 +95,8 @@ public function register(Request $request)
$this->validator($request->all())->validate();

$user = $this->create($request->all());
$this->createDefaultCategory($user);
$user = self::createApiToken($user);
$user = self::createDefaultCategory($user);

event(new UserRegistered($user));

Expand All @@ -113,4 +115,12 @@ public static function createDefaultCategory(User $user)

return $user;
}

public static function createApiToken(User $user)
{
$user->api_token = Uuid::uuid4();
$user->save();

return $user;
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"laravelcollective/html": "5.7.1",
"pda/pheanstalk": "3.2.1",
"predis/predis": "1.1.1",
"ramsey/uuid": "3.8.0",
"spatie/flysystem-dropbox": "1.0.6",
"spatie/laravel-backup": "5.11.3",
"spatie/laravel-paginateroute": "2.7.0",
Expand Down
3 changes: 3 additions & 0 deletions config/createuser.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Support\Facades\Hash;
use Ramsey\Uuid\Uuid;

return [
/*
Expand Down Expand Up @@ -42,6 +43,8 @@
],

'post_creation_fn' => function (\App\Models\User $user) {
$user = \App\Http\Controllers\Auth\RegisterController::createApiToken($user);

return \App\Http\Controllers\Auth\RegisterController::createDefaultCategory($user);
},

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

use App\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddApiTokenToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('api_token', 60);
});

// create unique tokens for existing users
$users = User::all();

$users->each(function (User $user) {
\App\Http\Controllers\Auth\RegisterController::createApiToken($user);
});

Schema::table('users', function (Blueprint $table) {
$table->string('api_token', 60)->unique()->change();
});
}

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

0 comments on commit a25d58b

Please sign in to comment.