Skip to content

Guang1234567/vapor4_auth_template

Repository files navigation

Auth Template

Documentation Team Chat MIT License Continuous Integration Swift 4.1

Usage

# 1)

brew install vapor

# 2)

vapor new demo_swift_server_vapor_auth --template https://github.com/Guang1234567/vapor4_auth_template

Peer by docker

repository/docker/lihansey/vapor4_auth_template

# 1)

╰─ docker pull lihansey/vapor4_auth_template:latest

# 2)

╰─ docker-compose up app

# 3) create database scheme and tables

╰─ docker-compose up migrate

# 4)

╰─ broswer http://127.0.0.1:8080

Develop on localhost

macOS

Serving HTTPS by nginx

  1. Certificate generation

    Also because HTTP/2 is a secure protocol by default, you'll need your own SSL certificate. You can generate a self-signed cert.pem and a cert.key files with the following command (fill out the details with some fake data and press enter).

        cd ./etc/nginx
        
        openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout cert.key -out cert.pem
  2. install nginx

    https://docs.vapor.codes/4.0/deploy/nginx/

        brew install nginx
  3. cfg ngnix

    server {
    listen 443 ssl;
    server_name localhost;
    ssl_certificate cert.pem;
    ssl_certificate_key cert.key;
    ssl_session_cache shared:SSL:50m;
    ssl_session_timeout 1d;
    #ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
    ssl_prefer_server_ciphers on;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_trusted_certificate cert.pem;
    ssl_stapling on;
    ssl_stapling_verify on;
    add_header Strict-Transport-Security max-age=15768000;
    root ~/dev_kit/sdk/swift_source/readdle/demo_swift_server_vapor_auth/Public/;
    location / {
    try_files $uri @proxy;
    }
    location @proxy {
    proxy_pass http://127.0.0.1:8080;
    proxy_pass_header Server;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass_header Server;
    proxy_connect_timeout 3s;
    proxy_read_timeout 10s;
    }
    }

    1. start nginx
        nginx -c ~/dev_kit/workspace/demo_swift_server_vapor_auth/etc/nginx/nginx.conf
  4. check HTTPS Env

    Using your smartphone which under the same wifi with your macbookpro to browse:

        http://<your macOS IP>:8080/
        
        
        For example (Dont copy!):
        http://10.0.0.28:8080/

    then will see:

         Welcome to nginx!
         
         If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
         
         For online documentation and support please refer to nginx.org.
         Commercial support is available at nginx.com.
         
         Thank you for using nginx.
  5. other command for nginx

    # quit nginx
    
    nginx -s quit
    
    
    # dry run and test
    
    nginx -c ~/dev_kit/workspace/demo_swift_server_vapor_auth/etc/nginx/nginx.conf -t
    

Start vapor web application and serving on http://127.0.0.1:8080

  swift build -Xswiftc -g -c debug && .build/debug/Run --log debug --env development.custom_name

note:

issue: Crashed when calling Bcrypt.hash on

  • macOS catalina 10.15.6 with xcode 11.6
  • macOS catalina 10.15.5 with xcode 11.5

then access vapor web application via HTTPS

  • on smartphone
 https://10.0.0.28/
 
 or
 
 https://10.0.0.28:443/
  • on PC
 https://127.0.0.1/
 
 or
 
 https://127.0.0.1:443/

then both will see:

  It works!

Test API via CLion with swift plugin

POST http://{{host}}/users HTTP/1.1
content-type: application/json
{
"name": "Vapor",
"email": "[email protected]",
"password": "secret",
"confirmPassword": "secret"
}

Ubuntu

// in the process ... ( I dont have the Ubuntu -_-|| )