Friday, December 2, 2016

Upgrade PHP version to PHP7 quickly on LAMP environment

Run Following commands:

$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update
$ sudo apt-get install -y php7.0
After that run following command:

sudo apt install php libapache2-mod-php
Now restart  Apache server :


sudo service apache2 stop
sudo service apache2 start
Repository required to install phpmyadmin

sudo add-apt-repository ppa:ondrej/php5-compat

Thursday, November 3, 2016

Laravel 5.3 default authentication

In this tutorial i'll give  basic idea of authentication system provided by laravel 5.3. You can find more details about authentication system in this video tutorial.

Authentication system is one of the core module of an web application. It will take a lot of time to develop an authentication system without security flaws.

Laravel 5.3 provides quick and easy to scaffold all of the routes and views you need for authentication.

Use following command  and laravel will setup everything for you.

php artisan make:auth
and make sure you ran following command which will required for registering new users and reset password.

php artisan migrate
Now we have everything running. If we browse our web application we'll see two link. One is Register and another one is Login .


Now if you want to create new user , click Register and you'll see following page:

Provide Necessary details and you will be redirect to user dashboard.


Laravel also generate default login page for you.


you can find all the routes related to laravel authentication system in auth() function on following directory:
vendor/laravel/framework/src/illuminate/Routing/Router.php 

Sunday, October 23, 2016

Api authorization with Passport in Laravel 5.3

This tutorial is basically an environment set up to understand how Passport works in Laravel 5.3.

Passport is a way to manage authentication for accessing API in Laravel. It is very easy to implement and user do not need to concern about background activities   of Oauth.

To implement Passport in local environment , we need two projects. One will be working as server, which authenticate a user and another one will be working as client, which will request for resource via API.


so i am using two laravel 5.3 instances as server and client.

Server is running on localhost:8000
Client is running on localhost:9000

Here is the instructions that must be followed to setup oAuth server on our Server(localhost:8000)

1. composer require laravel/passport

Register PassportServiceProvider in providers array on app.php file

2. Laravel\Passport\PassportServiceProvider::class,

Migrate tables related to Passport.

3. php artisan migrate

Create Encryption key to generate secure access key

4. php artisan passport:install

Add HasApiTokens traits is used in User model, which will provide some helper functions to play with access tokens.

5. <?php

namespace App;

use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use HasApiTokens, Notifiable;
}

Now in AuthSericeProvider's boot function add following codes to automatically set up required routes related to passport

6.  public function boot()
    {
        $this->registerPolicies();

        Passport::routes();
    }

Now in config/auth.php file set Passport as API driver


7. 'api' => [
        'driver' => 'passport',
        'provider' => 'users',
    ],

Now run following code to publish vue components related to Passport.


8. php artisan vendor:publish --tag=passport-components
Now add following code in app.js file


9. Vue.component(
    'passport-clients',
    require('./components/passport/Clients.vue')
);

Vue.component(
    'passport-authorized-clients',
    require('./components/passport/AuthorizedClients.vue')
);

Vue.component(
    'passport-personal-access-tokens',
    require('./components/passport/PersonalAccessTokens.vue')
);

Now we have to compile this by running following code

10.
   gulp watch



Now add following lines in the view page in order to give access to admin to create client, personal access token.


11.
<passport-clients></passport-clients>
<passport-authorized-clients></passport-authorized-clients>
<passport-personal-access-tokens></passport-personal-access-tokens>
Now our server configuration done. Now we'll create new client in order to give access.

12.


Now we'll setup our client

In Client's web.php file add following code :

1.
 use Illuminate\Http\Request;

Route::get('/', function () {
    $query = http_build_query([
        'client_id' => 12,        'redirect_uri' => 'http://localhost:9000/callback',        'response_type' => 'code',        'scope' => '',    ]);
    return redirect('http://localhost:8000/oauth/authorize?'.$query);});
Route::get('/callback', function (Request $request) {

    $http = new GuzzleHttp\Client;
    $response = $http->post('http://localhost:8000/oauth/token', [
        'form_params' => [
            'grant_type' => 'authorization_code',            'client_id' => 12,            'client_secret' => 'rfkixBY5v3SwnqwRM570yKHfgQMRNyNDd1WgOxWP',            'redirect_uri' => 'http://localhost:9000/callback',            'code' => $request->code,        ],    ]);
    return json_decode((string) $response->getBody(),true);    

});

So our basic setup done. Now if we'll try to access localhost:9000 we'll be redirect to following page:

2.


Now if we authorize we'll see access token as follow

3.

Now our system is working fine. If we go to localhost:8000 and refresh our browser , we'll see following view

4.


We can also create personal access token. To generate personal access token we need to create a Password grant client using following command:

1 . php artisan passport:client --password 

Now in our server application we can create personal access token.



Thursday, September 29, 2016

Python Connect Database Using peewee


First install peewee using pip

pip3 install peewee
Now we have to install MySQLdb or PyMySQL

To install PyMySQL run following command:
sudo pip3 install PyMySQL
Now our basic Configuration done.
Lets create a sample program to connect database and retrieve information from an existing table
import peewee
from peewee import *
db = MySQLDatabase('Word', user='root' , passwd='csecu')


class Words(peewee.Model):
    name = peewee.CharField()
    engdes = peewee.CharField()
    pages = peewee.IntegerField()

    class Meta:
        database = db


for words in Words.filter(name="bappu"):
    print(words.pages)
Lets breakdown :

db = MySQLDatabase('Word', user='root' , passwd='csecu')
This line is used to establish a database connection.

class Words(peewee.Model):
    name = peewee.CharField()
    engdes = peewee.CharField()
    pages = peewee.IntegerField()
Here class Words, represents that, we have a table name words in Word database and it has 3 columns. Which are name, engdes and pages 

class Meta:
        database = db

This means that Words model uses the "Word" database.
for words in Words.filter(name="bappu"):
    print(words.pages)
This is used to select all data where name = bappu and showing number of pages

Thursday, September 1, 2016

Up & Running With Gulp

Check Older version if installed

$ npm rm --global gulp
  Install Gulp Globally
$ npm install --global gulp-cli
Initializing Project directory
npm init
Installing Dependencies
npm install --save-dev gulp
Creatng gulpfile.js in root directory
var gulp = require('gulp');

gulp.task('default', function() {
  // place code for your default task here
});
Preprocessing with Gulp 

Compile Sass to CSS with the help of gulp-sass PKG

$ npm install gulp-sass --save-dev

 --save-dev flag to ensure that gulp-sass gets added to devDependencies in package.json Now add following line to gulpfile.js
var sass = require('gulp-sass');
  Now add following line in gulpfile.js
gulp.task('sass', function(){
    return gulp.src('css/sass/shuvrow.scss')
        .pipe(sass()) // Using gulp-sass
        .pipe(gulp.dest('css'))
});
Now add watch to change dynamically.
 Add following line on gulofile.js
gulp.task('watch', function(){
    gulp.watch('css/sass/shuvrow.scss', ['sass']);
    // Other watchers
})
Now run following command in terminal
gulp watch
Now you can see shuvrow.css file changes automatically.
Live Reloading => Auto sync 
Run Following command:

npm install browser-sync --save-dev
Add following line on gulpfile.js
var browserSync = require('browser-sync').create();
Now add following lines
gulp.task('browserSync', function() {
  browserSync.init({
    server: {
      baseDir: ''
    },
  })
})
Now update following code
gulp.task('sass', function(){
    return gulp.src('css/sass/main.scss')
        .pipe(sass()) // Using gulp-sass
        .pipe(gulp.dest('css'))
});
to
gulp.task('sass', function(){
    return gulp.src('css/sass/main.scss')
        .pipe(sass()) // Using gulp-sass
        .pipe(gulp.dest('css'))
        .pipe(browserSync.reload({
            stream: true
        }))
});
Now, we have to run both the watch and browserSync tasks at the same time for live-reloading
To do this update following codes
gulp.task('watch',function(){
    gulp.watch('css/sass/main.scss', ['sass']);
    // Other watchers
});
to
gulp.task('watch',['browserSync','sass'], function(){
    gulp.watch('css/sass/main.scss', ['sass']);
    // Other watchers
});
Now run gulp watch command on terminal to see all new changes on browser automatically.

Tuesday, August 23, 2016

Install laravel 5.3 in Ubuntu 14.04


Once again Laravel comes with many amazing features and i can't wait more to try this all out.

In this post i am going to write how to setup laravel 5.3 in Ubuntu 14.04.
I am planning on making video for each of this post. Here you can find more about installation of l5.3 on ubuntu
In laravel documentation, it says
  • PHP >= 5.6.4
required to install laravel 5.3.

So first run following command on terminal:

  • php -v
This will show you, your current php version.


As you can see, i have PHP version 5.5.9 , so i have to upgrade it to  >=v5.6.4

So to do this i have to run following commands:

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php5-5.6
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install php5
Now if i check my updated php version i can see this :


So my php version updated as i needed.

Now i have to run following command to install new Laravel 5.3 setup

composer create-project --prefer-dist laravel/laravel HighFive
So my installation is done.

Now i have to go to project directory and run following command
php artisan serve
Now if i go to browser and browse localhost:8000 , hurry laravel 5.3 is on..


Laravel Elixir Setup and Uses



Laravel Elixir provides a clean, fluent API for defining basic Gulp tasks for your Laravel application. Elixir supports several common CSS and JavaScript pre-processors, and even testing tools. Using method chaining, Elixir allows you to fluently define your asset pipeline. For example:
elixir(function(mix) {
    mix.sass('app.scss')
       .coffee('app.coffee');
});
Gulp is a task/build runner for development. It allows you to do a lot of stuff within your development workflow. You can compile sass files, uglify and compress js files and much more.

Installation & Setting Up

  • First Setup Node
    • Try Following command to check node version
      • node -v 
          
  • Install Gulp Globally
    • Try Following command 
      • npm install --global gulp-cli (for windows)
      • sudo npm install --global gulp-cli (for linux)
      • npm install -- (to install npm dependency: use sudo for linux do install all dependency)
After   Installing all dependency , run following code to notify all changes related to app.scss or any file your are willing to work.







  • gulp watch 
  • Lets say, we want to make change to app.scss and convert it to css file under public folder.
    To do this, first change 
    elixir(function(mix) {
        mix.sass('app.scss')
           .coffee('app.coffee');
    });
    to

    elixir(function(mix) {
        mix.sass('app.scss','public/style.css');
    });
    now change code in app.scss file and you'll see all changes in public/style.css file.