Coder Social home page Coder Social logo

token-server's Introduction

Token Server

  • TypeGraphQL

Ref

Todo

  • app init.
  • prettier, eslint
  • TypeORM, TypeGraphQL(resolver, Context)

Install

  • yarn add typeorm
  • yarn add -D eslint prettier typescript
  • yarn add -D eslint-config-airbnb-base eslint-plugin-import
  • yarn add -D eslint-config-prettier eslint-plugin-prettier
  • yarn
  • yarn upgrade-interactive --latest
  • yarn add express apollo-server-express graphql
  • yarn add -D @types/express @types/graphql
  • yarn add type-graphql bcryptjs
  • yarn add -D nodemon
  • yarn add jsonwebtoken
  • yarn add dotenv
  • yarn add cookie-parser
  • yarn add -D @types/cookie-parser

Code

1. Query의 response타입 설정.

/api/user/LoginResponse.ts

@ObjectType()
class LoginResponse {
  @Field(() => Boolean)
  ok: boolean;

  @Field(() => String, { nullable: true })
  error?: string;

  @Field(() => String, { nullable: true })
  token?: string;
}
2. Context를 통한 Requset처리
  1. MyContext.ts
// 요청 쿼리의 header에서 키 'x-jwt'의 데이터를 처리.
import { Request, Response } from 'express';

export interface MyContext {
  req: Request;
  res: Response;
};
  1. index.ts
(async () => {
  // ...
  const app = express();

  const apolloServer = new ApolloServer({
    schema: buildSchema({
      resolvers: [GetMyProfile],
      context: ({req, res}) => ({req, res})
    })
  });

  apolloServer.middlewares({ app });
})();
  1. EmailSignIn.ts
@Mutation(() => User)
async EmailSignIn(
  @Arg('email') email: string,
  @Arg('password') password: string,
  @Ctx() ctx: MyContext
): Promise<Boolean> {
  const { req } = ctx;
  const token: string | undefined = req.get('x-jwt');
  return Boolean(token);
}

Study

  1. typeorm 서버생성.
  • typeorm init --name "생성될 디렉터리 명" --database "디비 명(postgres)"
  1. typescript의 node환경 구성에 필요한것 tsconfig.json환경을 제공.
  • npx tsconfig.json
  1. yarn upgrade-interactive --latest
  • yarn 입력후 사용해야 됨.
  • 최신버전으로 다운 됨.

ETC

  1. Ctrl + Shift + H
  • Restful Client의 요청 history

token-server's People

Contributors

rhkdgns95 avatar

Watchers

James Cloos avatar  avatar

Forkers

masterh8887

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.