1.0.0 • Published 2 years ago

api42tunnell v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

api42tunnel

api42tunnel is an small package that support typescript to help you use 42Api

installation :

to install api42tunnel use :

npm i api42tunnel

how to use :

example of how to use api42tunnel on nestJs project

- Auth class
  • app.controller.ts :
import { Controller, Get, Req } from '@nestjs/common';
import { AppService } from './app.service';
import { Request } from 'express';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get("login")
  login(): string {
    const uid = ""; //  uid that you got where you register you application in intra
    const redirect_uri = "http%3A%2F%2Flocalhost%3A3000%2FgetToken"; // you redirection url incoded
    return `<a href='https://api.intra.42.fr/oauth/authorize?client_id=${uid}&redirect_uri=${redirect_uri}&response_type=code'>LOGIN WITH INTRA</a>` // this is url to login with intra
  }

  @Get("getToken")
  async getToken(@Req() request: Request)  {
    const response = await this.appService.getAccessToken(request.query.code);
    
    return response;
  }
}

first we have to set our controller, we have to create login method that we return a simple link from it that have our application id uid and our application redirect url redirect_uri , this link will redirect us to get autorization from the user, after we get autorized we will be redirected to redirect_uri with autorization code as parameter.

after that we catch that url with that url with getToken() method, and then we take the code from parameters and send it to getAccessToken() on appService, look at the example bellow.

  • app.service.ts
import { Injectable } from '@nestjs/common';
import { Auth } from 'api42tunnel';
import { TokenDto } from './DTOs/tokenDto';

@Injectable()
export class AppService {
  uid = "YOUR_USER_ID"; // uid that you get whene you create your application on the intra
  secret = "YOUR_SECRET"; // secret that you get whene you create your application on the intra
  redirect_url = "REDIRECTION_URL"; // the redirection url (you have to put it in redirection url whene you create your application, it is the url that you will be redirected to, whide authorization code when you authorize the application)

  async getAccessToken(code: string): Promise<string> {
    const tunnel  = new Auth({ uid: this.uid, secret : this.secret, redirect_url: this.redirect_url});
    const response: any = await tunnel.getToken(code);
    const token: TokenDto = response.data;
    return token.access_token;
  }
}

on out app.service.ts we have to import the class { Auth } from api42tunnel , then we have to instentiate an object from this class that takes and object as parameter, object defined bellow :

{
	uid: string,
	secret: string,
	redirect_url: string
}

the attributes of this object explained on the example above.

after that we have to call the method getToken() and give it the autorization code as parameter and it will return an access_token as a string.

- Tunnel class

to use the Tunnel class you have to import { Tunnel } from api42tunnel, the constructor of this class takes the access_token as parameter.

EXAMPLE :

import { Tunnel } from 'api42tunnel';

const tunnel = new Tunnel("YOUR_ACCESS_TOKEN");

tunnel.getUserById("USER ID AS NUMBER").then(response => {
	console.log(response);
}).catch(error => {
	console.log(error);
})