我想使用laravel,twilio和authy api进行电话号码验证。
我遵循了网站https://www.twilio.com/docs/sms
上提供的教程,但它一直向我显示此错误Unresolvable dependency resolving [Parameter #0 [ <required> $api_key ]] in class Authy\AuthyApi
这是我用户的模型:
<?php
namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password', 'country_code', 'phone_number'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
public function fullNumber()
{
return '+' . $this->country_code . $this->phone_number;
}
}
我的控制器:
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use App\User;
use Auth;
use Authy\AuthyApi as AuthyApi;
use DB;
use Hash;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;
use Twilio\Rest\Client;
class UserController extends Controller
{
/**
* Store a new user
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function createNewUser(Request $request, AuthyApi $authyApi)
{
$this->validate(
$request, [
'name' => 'required|string',
'email' => 'required|unique:users|email',
'password' => 'required',
'country_code' => 'required',
'phone_number' => 'required|numeric'
]
);
$values = $request->all();
$values['password'] = Hash::make($values['password']);
DB::beginTransaction();
$newUser = new User($values);
$newUser->save();
Auth::login($newUser);
$authyUser = $authyApi->registerUser(
$newUser->email,
$newUser->phone_number,
$newUser->country_code
);
if ($authyUser->ok()) {
$newUser->authy_id = $authyUser->id();
$newUser->save();
$request->session()->flash(
'status',
"User created successfully"
);
$sms = $authyApi->requestSms($newUser->authy_id);
DB::commit();
return redirect()->route('user-show-verify');
} else {
$errors = $this->getAuthyErrors($authyUser->errors());
DB::rollback();
return view('newUser', ['errors' => new MessageBag($errors)]);
}
}
/**
* This controller function shows the current user status
*
* @param Authenticatable $user Current user
* @return mixed Response view
*/
public function show(Authenticatable $user)
{
return view('showUser', ['user' => $user]);
}
/**
* This controller function handles the submission form
*
* @param Request $request Current User Request
* @param Authenticatable $user Current User
* @param AuthyApi $authyApi Authy Client
* @return mixed Response view
*/
public function verify(Request $request, Authenticatable $user,
AuthyApi $authyApi, Client $client)
{
$token = $request->input('token');
$verification = $authyApi->verifyToken($user->authy_id, $token);
if ($verification->ok()) {
$user->verified = true;
$user->save();
$this->sendSmsNotification($client, $user);
return redirect()->route('user-index');
} else {
$errors = $this->getAuthyErrors($verification->errors());
return view('verifyUser', ['errors' => new MessageBag($errors)]);
}
}
/**
* This controller function handles the verification code resent
*
* @param Request $request Current User Request
* @param Authenticatable $user Current User
* @param AuthyApi $authyApi Authy Client
* @return mixed Response view
*/
public function verifyResend(Request $request, Authenticatable $user,
AuthyApi $authyApi)
{
$sms = $authyApi->requestSms($user->authy_id);
if ($sms->ok()) {
$request->session()->flash(
'status',
'Verification code re-sent'
);
return redirect()->route('user-show-verify');
} else {
$errors = $this->getAuthyErrors($sms->errors());
return view('verifyUser', ['errors' => new MessageBag($errors)]);
}
}
private function getAuthyErrors($authyErrors)
{
$errors = [];
foreach ($authyErrors as $field => $message) {
array_push($errors, $field . ': ' . $message);
}
return $errors;
}
private function sendSmsNotification($client, $user)
{
$twilioNumber = config('services.twilio')['number'] or die(
"TWILIO_NUMBER is not set in the environment"
);
$messageBody = 'You did it! Signup complete :)';
$client->messages->create(
$user->fullNumber(), // Phone number which receives the message
[
"from" => $twilioNumber, // From a Twilio number in your account
"body" => $messageBody
]
);
}
}
迁移文件:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email',191)->unique();
$table->string('password', 60);
$table->string('phone_number');
$table->string('country_code');
$table->string('authy_id')->nullable();
$table->boolean('verified')->default(false);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
提供商
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Services_Twilio;
use Twilio\Rest\Client;
class TwilioRestClientProvider extends ServiceProvider
{
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->singleton(Client::class, function ($app) {
$accountSid = config('services.twilio')['accountSid']
or die("TWILIO_ACCOUNT_SID is not set in the environment");
$authToken = config('services.twilio')['authToken']
or die("TWILIO_AUTH_TOKEN is not set in the environment");
return new Client($accountSid, $authToken);
});
}
}
<?php
namespace App\Providers;
use Authy\AuthyApi as AuthyApi;
use Illuminate\Support\ServiceProvider;
class AuthyApiProvider extends ServiceProvider
{
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->singleton(AuthyApi::class, function ($app) {
$authyKey = getenv('AUTHY_API_KEY') or die(
"You must specify your api key for Authy. " .
"Visit https://dashboard.authy.com/"
);
return new AuthyApi($authyKey);
});
}
}
视图文件
@extends('layouts.master')
@section('title')
Sign Up
@endsection
@section('content')
<div class="container">
<h1>We're going to be *BEST* friends</h1>
<p> Thanks for your interest in signing up! Can you tell us a bit about yourself?</p>
{!! Form::open(['url' => route('user-create')]) !!}
<div class="form-group">
{!! Form::label('name') !!}
{!! Form::text('name', '', ['class' => 'form-control', 'placeholder' => 'Zingelbert Bembledack']) !!}
</div>
<div class="form-group">
{!! Form::label('email') !!}
{!! Form::text('email', '', ['class' => 'form-control', 'placeholder' => 'me@mydomain.com']) !!}
</div>
<div class="form-group">
{!! Form::label('password') !!}
{!! Form::password('password', ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('country_code', 'Country Code') !!}
{!! Form::text('country_code', '', ['class' => 'form-control', 'id' => 'authy-countries']) !!}
</div>
<div class="form-group">
{!! Form::label('phone_number', 'Phone number') !!}
{!! Form::text('phone_number', '', ['class' => 'form-control', 'id' => 'authy-cellphone']) !!}
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Sign up</button>
</div>
{!! Form::close() !!}
</div>
@endsection
环境文件
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:VVX5mc9IgZTovcUVBIh4JEcaJ7Yom53Aq3jTbKtj+LY=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=vmsdb
DB_USERNAME=root
DB_PASSWORD=
# Twilio API credentials
# Found at https://www.twilio.com/user/account/settings
TWILIO_ACCOUNT_SID=AC569005ea010eb698a910836c4833cc1b
TWILIO_AUTH_TOKEN=7c62c83f431b4ecc16c4e244728c092c
# Twilio phone number
# Purchase one at https://www.twilio.com/user/account/phone-numbers
TWILIO_NUMBER=+233238855067
# Authy Credentials
AUTHY_API_KEY=gXX0yRjwv684h2kpgmSouzyHWqkvfGZI
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
但是一直显示错误:
Unresolvable dependency resolving [Parameter #0 [ <required> $api_key ]] in class Authy\AuthyApi
我已经要求composer软件包中的authy和twilio,但是它仍然显示相同的错误,请寻求帮助。谢谢
答案 0 :(得分:1)
请在共享项目时删除您的凭据!
您似乎正在尝试使用getenv()从.env文件中提取信息,但这不是Laravel这样做的方式,并且可能会引起问题(https://laracasts.com/discuss/channels/laravel/cant-get-getenvkey?page=1)。
我建议在config / services.php中设置这些值,然后使用Laravel的config()方法(https://laravel.com/docs/5.8/configuration)来获取它们。
所以您的config / services.php看起来像这样:
return [
'authy' => [
'key' => env('AUTHY_API_KEY'),
],
];
,然后获取api密钥将类似于:
config('services.authy.key');