Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
semour
/
semour_web
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
f3006e7d
authored
Oct 31, 2022
by
杨树贤
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
注册接口
parent
3638edfd
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
47 additions
and
5 deletions
.idea/workspace.xml
app/Http/Controllers/Api/AuthApiController.php
app/Models/User.php
routes/api.php
.idea/workspace.xml
View file @
f3006e7d
...
...
@@ -4,8 +4,8 @@
<list
default=
"true"
id=
"fb90add0-1393-48c2-9f26-72365d42cd03"
name=
"变更"
comment=
""
>
<change
beforePath=
"$PROJECT_DIR$/.idea/workspace.xml"
beforeDir=
"false"
afterPath=
"$PROJECT_DIR$/.idea/workspace.xml"
afterDir=
"false"
/>
<change
beforePath=
"$PROJECT_DIR$/app/Http/Controllers/Api/AuthApiController.php"
beforeDir=
"false"
afterPath=
"$PROJECT_DIR$/app/Http/Controllers/Api/AuthApiController.php"
afterDir=
"false"
/>
<change
beforePath=
"$PROJECT_DIR$/app/Models/User.php"
beforeDir=
"false"
afterPath=
"$PROJECT_DIR$/app/Models/User.php"
afterDir=
"false"
/>
<change
beforePath=
"$PROJECT_DIR$/routes/api.php"
beforeDir=
"false"
afterPath=
"$PROJECT_DIR$/routes/api.php"
afterDir=
"false"
/>
<change
beforePath=
"$PROJECT_DIR$/routes/web.php"
beforeDir=
"false"
afterPath=
"$PROJECT_DIR$/routes/web.php"
afterDir=
"false"
/>
<change
beforePath=
"$PROJECT_DIR$/storage/app/.gitignore"
beforeDir=
"false"
afterPath=
"$PROJECT_DIR$/storage/app/.gitignore"
afterDir=
"false"
/>
<change
beforePath=
"$PROJECT_DIR$/storage/app/public/.gitignore"
beforeDir=
"false"
afterPath=
"$PROJECT_DIR$/storage/app/public/.gitignore"
afterDir=
"false"
/>
<change
beforePath=
"$PROJECT_DIR$/storage/framework/.gitignore"
beforeDir=
"false"
afterPath=
"$PROJECT_DIR$/storage/framework/.gitignore"
afterDir=
"false"
/>
...
...
app/Http/Controllers/Api/AuthApiController.php
View file @
f3006e7d
...
...
@@ -2,24 +2,52 @@
namespace
App\Http\Controllers\Api
;
use
App\Models\User
;
use
Illuminate\Auth\Events\Registered
;
use
Illuminate\Foundation\Auth\Access\AuthorizesRequests
;
use
Illuminate\Foundation\Auth\RedirectsUsers
;
use
Illuminate\Foundation\Auth\RegistersUsers
;
use
Illuminate\Foundation\Auth\ThrottlesLogins
;
use
Illuminate\Foundation\Bus\DispatchesJobs
;
use
Illuminate\Foundation\Validation\ValidatesRequests
;
use
Illuminate\Http\JsonResponse
;
use
Illuminate\Http\Request
;
use
Illuminate\Support\Facades\Validator
;
class
AuthApiController
extends
Controller
{
use
RedirectsUsers
,
ThrottlesLogins
;
use
ThrottlesLogins
,
RegistersUsers
;
public
function
register
(
Request
$request
)
{
$validator
=
Validator
::
make
(
$request
->
all
(),
[
'name'
=>
[
'required'
,
'string'
,
'max:255'
],
'email'
=>
[
'required'
,
'string'
,
'email'
,
'max:255'
,
'unique:users'
],
'password'
=>
[
'required'
,
'string'
,
'min:8'
,
'confirmed'
],
]);
if
(
$validator
->
fails
())
{
return
$this
->
setError
(
$validator
->
errors
()
->
first
());
}
event
(
new
Registered
(
$user
=
User
::
createUser
(
$request
->
all
())));
$this
->
guard
()
->
login
(
$user
);
if
(
$response
=
$this
->
registered
(
$request
,
$user
))
{
return
$this
->
setSuccess
(
'register success'
);
}
return
$this
->
setError
(
'register failure'
);
}
public
function
login
(
Request
$request
)
{
$validator
=
Validator
::
make
(
$request
->
all
(),
[
'email'
=>
'required|string|email|max:255'
,
'password'
=>
'required|string|min:8
|confirmed
'
,
'password'
=>
'required|string|min:8'
,
],
[
'password.min'
=>
'Password must be at least 8 characters long.'
]);
...
...
app/Models/User.php
View file @
f3006e7d
...
...
@@ -5,6 +5,7 @@ namespace App\Models;
use
Illuminate\Contracts\Auth\MustVerifyEmail
;
use
Illuminate\Foundation\Auth\User
as
Authenticatable
;
use
Illuminate\Notifications\Notifiable
;
use
Illuminate\Support\Facades\Hash
;
class
User
extends
Authenticatable
implements
MustVerifyEmail
{
...
...
@@ -16,7 +17,9 @@ class User extends Authenticatable implements MustVerifyEmail
* @var array
*/
protected
$fillable
=
[
'name'
,
'email'
,
'password'
,
'name'
,
'email'
,
'password'
,
];
/**
...
...
@@ -25,7 +28,8 @@ class User extends Authenticatable implements MustVerifyEmail
* @var array
*/
protected
$hidden
=
[
'password'
,
'remember_token'
,
'password'
,
'remember_token'
,
];
/**
...
...
@@ -36,4 +40,13 @@ class User extends Authenticatable implements MustVerifyEmail
protected
$casts
=
[
'email_verified_at'
=>
'datetime'
,
];
public
static
function
createUser
(
$data
)
{
return
User
::
create
([
'name'
=>
$data
[
'name'
],
'email'
=>
$data
[
'email'
],
'password'
=>
Hash
::
make
(
$data
[
'password'
]),
]);
}
}
routes/api.php
View file @
f3006e7d
...
...
@@ -16,6 +16,7 @@ use Illuminate\Support\Facades\Route;
Route
::
middleware
([])
->
namespace
(
'Api'
)
->
group
(
function
()
{
Route
::
POST
(
'/user/login'
,
'AuthApiController@login'
);
Route
::
POST
(
'/user/register'
,
'AuthApiController@register'
);
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment