Skip to content

Commit

Permalink
feat(login) login now returns jwt
Browse files Browse the repository at this point in the history
  • Loading branch information
FerLuisxd committed Apr 9, 2020
1 parent ca6da18 commit a9c30a6
Show file tree
Hide file tree
Showing 17 changed files with 268 additions and 58 deletions.
3 changes: 2 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
MONGO_SRV = 'mongodb+srv://SRN:123456789123@cubiculospool-oxewp.mongodb.net/CubiculosPoolDev?retryWrites=true&w=majority'
MONGO_SRV = 'mongodb+srv://SRN:123456789123@cubiculospool-oxewp.mongodb.net/CubiculosPoolDev?retryWrites=true&w=majority'
JWT_SECRET = 'hArDT0D3C0D3PAZZW0RD'
4 changes: 4 additions & 0 deletions .stignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.git
/node_modules
/.vscode
/images
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM node:13 AS base

WORKDIR /app
RUN npm install
RUN npm run build
COPY dist/main.js dist/main.js

#################################

FROM base AS dev

COPY bashrc /root/.bashrc
RUN npm install -g nodemon

#################################

FROM base AS prod

EXPOSE 8080
CMD node app.js
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.PHONY: push
push:
okteto build -t okteto/upc-pool:node-dev --target dev .
okteto build -t okteto/upc-pool:node .
5 changes: 5 additions & 0 deletions bashrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cat << EOF
Welcome to your development environment. Happy coding!
EOF

export PS1="\[\e[32m\]okteto\[\e[m\]> "
33 changes: 33 additions & 0 deletions k8s.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: upc-pool
spec:
replicas: 1
selector:
matchLabels:
app: upc-pool
template:
metadata:
labels:
app: upc-pool
spec:
containers:
- image: node
name: upc-pool

---

apiVersion: v1
kind: Service
metadata:
name: upc-pool
annotations:
dev.okteto.com/auto-ingress: "true"
spec:
type: ClusterIP
ports:
- name: "upc-pool"
port: 3000
selector:
app: upc-pool
12 changes: 12 additions & 0 deletions okteto.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: upc-pool
image: okteto/node
command: ["npm","install"]
workdir: /usr/
environment:
- MONGO_SRV=mongodb+srv://SRN:123456789123@cubiculospool-oxewp.mongodb.net/CubiculosPoolDev?retryWrites=true&w=majority
- environment=development
forward:
- 3000:8000
- 9229:9229
persistentVolume:
enabled: false
91 changes: 91 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"class-transformer": "^0.2.3",
"class-validator": "^0.11.1",
"dotenv": "^8.2.0",
"jsonwebtoken": "^8.5.1",
"lightning-pool": "^2.1.3",
"mongoose": "^5.9.7",
"puppeteer": "^2.1.1",
Expand Down
4 changes: 2 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { AppService } from './app.service';
import { UserModule } from './modules/user/user.module';
import { AuthModule } from './modules/auth/auth.module';
import { MongooseModule } from '@nestjs/mongoose';
require('dotenv').config()

require('dotenv').config()
@Module({
imports: [MongooseModule.forRoot(process.env.MONGO_SRV,{useNewUrlParser: true,useUnifiedTopology: true}),UserModule, AuthModule],
controllers: [AppController],
providers: [AppService],
providers: [AppService]
})
export class AppModule {}
1 change: 0 additions & 1 deletion src/modules/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Module } from '@nestjs/common';
import { AuthController } from './auth.controller';
import { AuthService } from './auth.service';
import { UserModule } from '../user/user.module';
import { UserService } from '../user/user.service';

@Module({
imports:[UserModule],
Expand Down
101 changes: 47 additions & 54 deletions src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,62 @@ import { async } from 'rxjs/internal/scheduler/async';
import { AuthDto } from './auth.entity';
import { UserService } from '../user/user.service';
import { User } from '../user/user.entity';
const { Pool } = require('lightning-pool');
import { Pool } from 'lightning-pool';
import { puppetterLogin } from './utils/puppetter';
import { JWTsign } from './utils/jwt';
// const puppeteer = require('puppeteer');
/* eslint-disable prefer-const*/

@Injectable()
export class AuthService {
puppeteerInstance: Browser
puppeteerPage: Page
puppeteerPool
constructor(private userService: UserService) {
constructor(private readonly userService: UserService) {
this.createFactory()
}

async upbWebTestPool(userCode, password) {
let page: Page = await this.puppeteerPool.acquire()
try {
let response = await puppetterLogin(page,userCode,password)
this.puppeteerPool.release(page)
if (response)
throw new BadRequestException(response);
return {
status:200
}
} catch (error) {
this.puppeteerPool.release(page)
throw error
}
}

async createUser(userCode,password){
let user:User = new User({userCode,password})
let response = await this.userService.saveNewUser(user)
return response
}

async loginUser() {
return
}
async loginUserExp(body:AuthDto) {
try {
let response = await this.upbWebTestPool(body.userCode, body.password)
if(response.status== 200){
let createUser = await this.createUser(body.userCode,body.password)
console.log(createUser)
let jwt = JWTsign(createUser)
return jwt
}
} catch (error) {
console.error(this.loginUserExp.name,error);
throw error
}

}

createFactory() {
const factory = {
create: async function (opts) {
Expand All @@ -35,13 +79,10 @@ export class AuthService {
reset: function (client: Page) {
console.debug('Reseting Instance')
return client.goto('https://aulavirtual.upc.edu.pe/',{ timeout:6000})
},
validate: function (client) {

}
};

var opts = {
const opts = {
max: 3,
min: 1,
minIdle: 2
Expand All @@ -50,52 +91,4 @@ export class AuthService {
this.puppeteerPool = new Pool(factory, opts)
}

async upbWebTestPool(username, password) {
let page: Page = await this.puppeteerPool.acquire()
try {
await page.focus('#user_id');
await page.keyboard.type(username);
await page.focus('#password');
await page.keyboard.type(password);
await page.click('#entry-login');
await page.waitFor(200);
let response = await page.evaluate(() => {
try {
return document.getElementById('loginErrorMessage').textContent;
} catch (e) {
return null;
}
});
console.log('resp', response);
this.puppeteerPool.release(page)
if (response)
throw new BadRequestException(response);
return {
statusCode: 200,
message: 'Ok'
}
} catch (error) {
this.puppeteerPool.release(page)
throw error
}
}

upcApi() {

}

async loginUser() {

let response = await this.upcApi()
return
}
async loginUserExp(body:AuthDto) {
let response = await this.upbWebTestPool(body.userCode, body.password)
if(response.statusCode == 200){
let user = new User()
//this.userService.saveNewUser()
}
return response
}

}
Loading

0 comments on commit a9c30a6

Please sign in to comment.