Skip to content

Commit

Permalink
refactor: one package support multiple laravel version. #13
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkcup committed Jun 8, 2019
1 parent 5ec3a89 commit 5c6714f
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 18 deletions.
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
# Laravel Make Auth Socialite

Automatically generate database, pages and routes for Laravel Socialite. Just like `php artisan make:auth`.
Automatically generate database, pages, and routes for Laravel Socialite. Just like `php artisan make:auth`.

supporting Laravel 5.5, 5.6 and 5.7!
Login with multiple providers using the same email will be determined as one user.

When logged in, you can link all providers to the current user, and login with them next time.

Special handling for [WeChat](https://sinkcup.github.io/laravel-socialite-wechat-login).

supporting Laravel 5.5 and 5.8!

## install

```
php artisan make:auth
composer require sinkcup/laravel-make-auth-socialite
php artisan make:auth-socialite --force
php artisan migrate
```

## config

add providers to `config/services.php`:
add to `config/services.php`:

```
'github' => [
Expand All @@ -25,7 +30,7 @@ add providers to `config/services.php`:
],
```

ENV:
add to `.env`:

```
AUTH_SOCIAL_LOGIN_PROVIDERS=Facebook,GitHub,Google
Expand All @@ -36,7 +41,7 @@ GITHUB_CALLBACK_URL=http://laravel-demo.localhost/login/github/callback

## screenshots

![Login page](https://user-images.githubusercontent.com/4971414/50548717-bac5f100-0c8c-11e9-974a-45dfbe1c41da.png)
![GitHub OAuth Login](https://user-images.githubusercontent.com/4971414/50548725-d3cea200-0c8c-11e9-9b01-9b949bcb6b4d.png)
![logged in](https://user-images.githubusercontent.com/4971414/50548746-24de9600-0c8d-11e9-8262-213ffa1309be.png)
![database](https://user-images.githubusercontent.com/4971414/50548808-f2816880-0c8d-11e9-8227-d8128f040c30.png)
![Laravel Socialite Login page](https://user-images.githubusercontent.com/4971414/59020731-2a17c080-887d-11e9-8cc7-c8c46f97dd1b.png)
![GitHub OAuth Login](https://user-images.githubusercontent.com/4971414/59006611-764f0a80-8855-11e9-9ac9-0f4de8ff6e77.png)
![Laravel Socialite Profile page and Linked Accounts](https://user-images.githubusercontent.com/4971414/59092834-120b7400-8945-11e9-8b1d-ae50c862e6a8.png)
![Laravel Socialite link multiple providers to one user](https://user-images.githubusercontent.com/4971414/59086178-876e4900-8933-11e9-8dad-e2a449a5689e.png)
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"name": "sinkcup/laravel-make-auth-socialite",
"description": "Automatically generate database, pages and routes for Laravel Socialite",
"description": "Automatically generate database, pages, and routes for Laravel Socialite",
"type": "library",
"require": {
"laravel/framework": "^5.7",
"laravel/socialite": "^4.0",
"doctrine/dbal": "^2.9"
"laravel/framework": "~5.5.0|~5.8.0|~5.9.0",
"laravel/socialite": "~3.3.0|~4.1.0",
"doctrine/dbal": "^2.9",
"phlak/semver": "^2.0"
},
"license": "MIT",
"authors": [
Expand Down
11 changes: 6 additions & 5 deletions src/Commands/MakeAuthSocialite.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Console\Command;
use Illuminate\Console\DetectsApplicationNamespace;
use PHLAK\SemVer\Version;

class MakeAuthSocialite extends Command
{
Expand Down Expand Up @@ -102,11 +103,11 @@ protected function exportViews()
continue;
}
}

copy(
__DIR__.'/stubs/make/views/'.$key,
$view
);
$version = new Version(app()->version());
$path = __DIR__.'/stubs/make/views/';
$file_for_version = $key.'_'.$version->major.'.'.$version->minor;
$file_path = file_exists($path.$file_for_version) ? $path.$file_for_version : $path.$key;
copy($file_path, $view);
}
}

Expand Down
103 changes: 103 additions & 0 deletions src/Commands/stubs/make/views/auth/login.stub_5.5
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
@extends('layouts.app')

@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Social Login</div>

<div class="panel-body text-center">
@if (in_array('wechat_web', $social_login_providers))
<script src="https://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script>
<div class="card-text" id="login_container"></div>
<script>
var obj = new WxLogin({
self_redirect: false,
id: "login_container",
appid: "{{ config('services.wechat_web.client_id') }}",
scope: "snsapi_login",
redirect_uri: "{{ config('services.wechat_web.redirect') }}",
state: "{{ csrf_token() }}",
style: "",
href: ""
});
</script>
@php
unset($social_login_providers[array_search('wechat_web', $social_login_providers)]);
@endphp
@endif

<ul class="list-inline">
@foreach ($social_login_providers as $provider)
<li>
<a class="nav-link btn {{ $errors->has(strtolower($provider)) ? 'btn-danger' : 'btn-primary' }}" href="/login/{{ strtolower($provider) }}" role="button">{{ __($provider) }}</a>
</li>
@endforeach
</ul>
</div>
</div>

<div class="panel panel-default">
<div class="panel-heading">Login</div>

<div class="panel-body">
<form class="form-horizontal" method="POST" action="{{ route('login') }}">
{{ csrf_field() }}

<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>

<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>

@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>

<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>

<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>

@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>

<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<div class="checkbox">
<label>
<input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me
</label>
</div>
</div>
</div>

<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Login
</button>

<a class="btn btn-link" href="{{ route('password.request') }}">
Forgot Your Password?
</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
81 changes: 81 additions & 0 deletions src/Commands/stubs/make/views/user/profile_edit.stub_5.5
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
@extends('layouts.app')

@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">{{ __('Social Login') }}</div>

<div class="panel-body">
<form method="POST" action="{{ route('profile.update') }}">
{{ csrf_field() }}
{{ method_field('PUT') }}

<div class="form-group row">
@if ($user->avatar)
<label for="avatar" class="col-md-4 col-form-label text-right">{{ __('Avatar') }}</label>
<div class="col-md-6">
<img class="img-thumbnail p-3" src="{{ $user->avatar }}" alt="{{ $user->name }} avatar">
</div>
@endif

<label for="name" class="col-md-4 col-form-label text-right">{{ __('Name') }}</label>

<div class="col-md-6">
<input id="name" type="text" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" name="name" value="{{ old('name') ?: $user->name }}" required autofocus>

@if ($errors->has('name'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
</div>

<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-right">{{ __('E-Mail Address') }}</label>

<div class="col-md-6">
<input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') ?: $user->email }}" required>

@if ($errors->has('email'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>

<div class="form-group row">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
{{ __('Update') }}
</button>
</div>
</div>
</form>
</div>
</div>

<div class="panel panel-default">
<div class="panel-heading">{{ __('Linked Accounts') }}</div>

<div class="panel-body text-center">
<ul class="list-inline">
@foreach ($social_login_providers as $provider)
<li>
@if (in_array(strtolower($provider), $linked_providers))
<a class="btn btn-success disabled" href="#" role="button">{{ __($provider) }}</a>
@else
<a class="btn btn-primary" href="/login/{{ strtolower(str_replace('_', '-', $provider)) }}" role="button">{{ __($provider) }}</a>
@endif
</li>
@endforeach
</ul>
</div>
</div>
</div>
</div>
</div>
@endsection

0 comments on commit 5c6714f

Please sign in to comment.